]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/include/lldb/Symbol/ClangASTType.h
Merge ^/head r275478 through r275622.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / include / lldb / Symbol / ClangASTType.h
1 //===-- ClangASTType.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_ClangASTType_h_
11 #define liblldb_ClangASTType_h_
12
13 #include <string>
14 #include "lldb/lldb-private.h"
15 #include "lldb/Core/ClangForward.h"
16 #include "clang/AST/Type.h"
17
18 namespace lldb_private {
19
20 //----------------------------------------------------------------------
21 // A class that can carry around a clang ASTContext and a opaque clang 
22 // QualType. A clang::QualType can be easily reconstructed from an
23 // opaque clang type and often the ASTContext is needed when doing 
24 // various type related tasks, so this class allows both items to travel
25 // in a single very lightweight class that can be used. There are many
26 // static equivalents of the member functions that allow the ASTContext
27 // and the opaque clang QualType to be specified for ease of use and
28 // to avoid code duplication.
29 //----------------------------------------------------------------------
30 class ClangASTType
31 {
32 public:
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         eTypeIsScalar           = (1u << 17),
52         eTypeIsInteger          = (1u << 18),
53         eTypeIsFloat            = (1u << 19),
54         eTypeIsComplex          = (1u << 20),
55         eTypeIsSigned           = (1u << 21)
56     };
57     
58
59     //----------------------------------------------------------------------
60     // Constructors and Destructors
61     //----------------------------------------------------------------------
62     ClangASTType (clang::ASTContext *ast_context, lldb::clang_type_t type) :
63         m_type (type),
64         m_ast  (ast_context) 
65     {
66     }
67
68     ClangASTType (clang::ASTContext *ast_context, clang::QualType qual_type);
69
70     ClangASTType (const ClangASTType &rhs) :
71         m_type (rhs.m_type),
72         m_ast  (rhs.m_ast)
73     {
74     }
75     
76     ClangASTType () :
77         m_type (0),
78         m_ast  (0)
79     {
80     }
81     
82     ~ClangASTType();
83     
84     //----------------------------------------------------------------------
85     // Operators
86     //----------------------------------------------------------------------
87
88     const ClangASTType &
89     operator= (const ClangASTType &rhs)
90     {
91         m_type = rhs.m_type;
92         m_ast = rhs.m_ast;
93         return *this;
94     }
95     
96
97     //----------------------------------------------------------------------
98     // Tests
99     //----------------------------------------------------------------------
100
101     explicit operator bool () const
102     {
103         return m_type != NULL && m_ast != NULL;
104     }
105     
106     bool
107     operator < (const ClangASTType &rhs) const
108     {
109         if (m_ast == rhs.m_ast)
110             return m_type < rhs.m_type;
111         return m_ast < rhs.m_ast;
112     }
113
114     bool
115     IsValid () const
116     {
117         return m_type != NULL && m_ast != NULL;
118     }
119     
120     bool
121     IsArrayType (ClangASTType *element_type,
122                  uint64_t *size,
123                  bool *is_incomplete) const;
124
125     bool
126     IsArrayOfScalarType () const;
127
128     bool
129     IsAggregateType () const;
130     
131     bool
132     IsBeingDefined () const;
133
134     bool
135     IsCharType () const;
136
137     bool
138     IsCompleteType () const;
139     
140     bool
141     IsConst() const;
142     
143     bool
144     IsCStringType (uint32_t &length) const;
145
146     bool
147     IsCXXClassType () const;
148     
149     bool
150     IsDefined() const;
151     
152     bool
153     IsFloatingPointType (uint32_t &count, bool &is_complex) const;
154
155     bool
156     IsFunctionType (bool *is_variadic_ptr = NULL) const;
157
158     uint32_t
159     IsHomogeneousAggregate (ClangASTType* base_type_ptr) const;
160
161     size_t
162     GetNumberOfFunctionArguments () const;
163     
164     ClangASTType
165     GetFunctionArgumentAtIndex (const size_t index);
166     
167     bool
168     IsVariadicFunctionType () const;
169
170     bool
171     IsFunctionPointerType () const;
172     
173     bool
174     IsIntegerType (bool &is_signed) const;
175
176     bool
177     IsObjCClassType () const;
178     
179     bool
180     IsObjCClassTypeAndHasIVars (bool check_superclass) const;
181     
182     bool
183     IsObjCObjectOrInterfaceType () const;
184
185     bool
186     IsObjCObjectPointerType (ClangASTType *target_type = NULL);
187     
188     bool
189     IsPolymorphicClass () const;
190
191     bool
192     IsPossibleCPlusPlusDynamicType (ClangASTType *target_type = NULL) const
193     {
194         return IsPossibleDynamicType (target_type, true, false);
195     }
196     
197     bool
198     IsPossibleDynamicType (ClangASTType *target_type, // Can pass NULL
199                            bool check_cplusplus,
200                            bool check_objc) const;
201
202
203     bool
204     IsPointerToScalarType () const;
205     
206     bool
207     IsRuntimeGeneratedType () const;
208     
209     bool
210     IsPointerType (ClangASTType *pointee_type = NULL) const;
211     
212     bool
213     IsPointerOrReferenceType (ClangASTType *pointee_type = NULL) const;
214     
215     bool
216     IsReferenceType (ClangASTType *pointee_type = nullptr, bool* is_rvalue = nullptr) const;
217     
218     bool
219     IsScalarType () const;
220     
221     bool
222     IsTypedefType () const;
223
224     bool
225     IsVoidType () const;
226
227     bool
228     GetCXXClassName (std::string &class_name) const;
229     
230     bool
231     GetObjCClassName (std::string &class_name);
232     
233
234     //----------------------------------------------------------------------
235     // Type Completion
236     //----------------------------------------------------------------------
237     
238     bool
239     GetCompleteType () const;
240
241     //----------------------------------------------------------------------
242     // AST related queries
243     //----------------------------------------------------------------------
244
245     size_t
246     GetPointerByteSize () const;
247     
248     //----------------------------------------------------------------------
249     // Accessors
250     //----------------------------------------------------------------------
251     
252     clang::ASTContext *
253     GetASTContext() const
254     {
255         return m_ast;
256     }
257     
258     ConstString
259     GetConstQualifiedTypeName () const;
260
261     ConstString
262     GetConstTypeName () const;
263     
264     ConstString
265     GetTypeName () const;
266
267     ConstString
268     GetDisplayTypeName () const;
269
270     uint32_t
271     GetTypeInfo (ClangASTType *pointee_or_element_clang_type = NULL) const;
272     
273     lldb::LanguageType
274     GetMinimumLanguage ();
275
276     lldb::clang_type_t
277     GetOpaqueQualType() const
278     {
279         return m_type;
280     }
281
282     lldb::TypeClass
283     GetTypeClass () const;
284     
285     void
286     SetClangType (clang::ASTContext *ast, lldb::clang_type_t type)
287     {
288         m_ast = ast;
289         m_type = type;
290     }
291
292     void
293     SetClangType (clang::ASTContext *ast, clang::QualType qual_type);
294
295     unsigned
296     GetTypeQualifiers() const;
297     
298     //----------------------------------------------------------------------
299     // Creating related types
300     //----------------------------------------------------------------------
301
302     ClangASTType
303     AddConstModifier () const;
304
305     ClangASTType
306     AddRestrictModifier () const;
307
308     ClangASTType
309     AddVolatileModifier () const;
310     
311     // Using the current type, create a new typedef to that type using "typedef_name"
312     // as the name and "decl_ctx" as the decl context.
313     ClangASTType
314     CreateTypedefType (const char *typedef_name,
315                        clang::DeclContext *decl_ctx) const;
316     
317     ClangASTType
318     GetArrayElementType (uint64_t *stride = nullptr) const;
319     
320     ClangASTType
321     GetCanonicalType () const;
322     
323     ClangASTType
324     GetFullyUnqualifiedType () const;
325     
326     // Returns -1 if this isn't a function of if the function doesn't have a prototype
327     // Returns a value >= 0 if there is a prototype.
328     int
329     GetFunctionArgumentCount () const;
330
331     ClangASTType
332     GetFunctionArgumentTypeAtIndex (size_t idx);
333
334     ClangASTType
335     GetFunctionReturnType () const;
336     
337     ClangASTType
338     GetLValueReferenceType () const;
339     
340     ClangASTType
341     GetNonReferenceType () const;
342
343     ClangASTType
344     GetPointeeType () const;
345     
346     ClangASTType
347     GetPointerType () const;
348     
349     ClangASTType
350     GetRValueReferenceType () const;
351
352     // If the current object represents a typedef type, get the underlying type
353     ClangASTType
354     GetTypedefedType () const;
355
356     ClangASTType
357     RemoveFastQualifiers () const;
358     
359     //----------------------------------------------------------------------
360     // Create related types using the current type's AST
361     //----------------------------------------------------------------------
362     ClangASTType
363     GetBasicTypeFromAST (lldb::BasicType basic_type) const;
364
365     //----------------------------------------------------------------------
366     // Exploring the type
367     //----------------------------------------------------------------------
368
369     uint64_t
370     GetByteSize () const;
371
372     uint64_t
373     GetBitSize () const;
374
375     lldb::Encoding
376     GetEncoding (uint64_t &count) const;
377     
378     lldb::Format
379     GetFormat () const;
380     
381     size_t
382     GetTypeBitAlign () const;
383
384     uint32_t
385     GetNumChildren (bool omit_empty_base_classes) const;
386
387     lldb::BasicType
388     GetBasicTypeEnumeration () const;
389
390     static lldb::BasicType
391     GetBasicTypeEnumeration (const ConstString &name);
392
393     uint32_t
394     GetNumDirectBaseClasses () const;
395     
396     uint32_t
397     GetNumVirtualBaseClasses () const;
398     
399     uint32_t
400     GetNumFields () const;
401     
402     ClangASTType
403     GetDirectBaseClassAtIndex (size_t idx,
404                                uint32_t *bit_offset_ptr) const;
405     
406     ClangASTType
407     GetVirtualBaseClassAtIndex (size_t idx,
408                                 uint32_t *bit_offset_ptr) const;
409     
410     ClangASTType
411     GetFieldAtIndex (size_t idx,
412                      std::string& name,
413                      uint64_t *bit_offset_ptr,
414                      uint32_t *bitfield_bit_size_ptr,
415                      bool *is_bitfield_ptr) const;
416     
417     uint32_t
418     GetIndexOfFieldWithName (const char* name,
419                              ClangASTType* field_clang_type = NULL,
420                              uint64_t *bit_offset_ptr = NULL,
421                              uint32_t *bitfield_bit_size_ptr = NULL,
422                              bool *is_bitfield_ptr = NULL) const;
423     
424     uint32_t
425     GetNumPointeeChildren () const;
426     
427     ClangASTType
428     GetChildClangTypeAtIndex (ExecutionContext *exe_ctx,
429                               size_t idx,
430                               bool transparent_pointers,
431                               bool omit_empty_base_classes,
432                               bool ignore_array_bounds,
433                               std::string& child_name,
434                               uint32_t &child_byte_size,
435                               int32_t &child_byte_offset,
436                               uint32_t &child_bitfield_bit_size,
437                               uint32_t &child_bitfield_bit_offset,
438                               bool &child_is_base_class,
439                               bool &child_is_deref_of_parent,
440                               ValueObject *valobj) const;
441     
442     // Lookup a child given a name. This function will match base class names
443     // and member member names in "clang_type" only, not descendants.
444     uint32_t
445     GetIndexOfChildWithName (const char *name,
446                              bool omit_empty_base_classes) const;
447     
448     // Lookup a child member given a name. This function will match member names
449     // only and will descend into "clang_type" children in search for the first
450     // member in this class, or any base class that matches "name".
451     // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
452     // so we catch all names that match a given child name, not just the first.
453     size_t
454     GetIndexOfChildMemberWithName (const char *name,
455                                    bool omit_empty_base_classes,
456                                    std::vector<uint32_t>& child_indexes) const;
457     
458     size_t
459     GetNumTemplateArguments () const;
460     
461     ClangASTType
462     GetTemplateArgument (size_t idx,
463                          lldb::TemplateArgumentKind &kind) const;
464
465
466     //----------------------------------------------------------------------
467     // Modifying RecordType
468     //----------------------------------------------------------------------
469     clang::FieldDecl *
470     AddFieldToRecordType (const char *name,
471                           const ClangASTType &field_type,
472                           lldb::AccessType access,
473                           uint32_t bitfield_bit_size);
474     
475     void
476     BuildIndirectFields ();
477     
478     clang::VarDecl *
479     AddVariableToRecordType (const char *name,
480                              const ClangASTType &var_type,
481                              lldb::AccessType access);
482
483     clang::CXXMethodDecl *
484     AddMethodToCXXRecordType (const char *name,
485                               const ClangASTType &method_type,
486                               lldb::AccessType access,
487                               bool is_virtual,
488                               bool is_static,
489                               bool is_inline,
490                               bool is_explicit,
491                               bool is_attr_used,
492                               bool is_artificial);
493     
494     // C++ Base Classes
495     clang::CXXBaseSpecifier *
496     CreateBaseClassSpecifier (lldb::AccessType access,
497                               bool is_virtual,
498                               bool base_of_class);
499     
500     static void
501     DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
502                                unsigned num_base_classes);
503     
504     bool
505     SetBaseClassesForClassType (clang::CXXBaseSpecifier const * const *base_classes,
506                                 unsigned num_base_classes);
507     
508
509     bool
510     SetObjCSuperClass (const ClangASTType &superclass_clang_type);
511     
512     bool
513     AddObjCClassProperty (const char *property_name,
514                           const ClangASTType &property_clang_type,
515                           clang::ObjCIvarDecl *ivar_decl,
516                           const char *property_setter_name,
517                           const char *property_getter_name,
518                           uint32_t property_attributes,
519                           ClangASTMetadata *metadata);
520
521     clang::ObjCMethodDecl *
522     AddMethodToObjCObjectType (const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
523                                const ClangASTType &method_clang_type,
524                                lldb::AccessType access,
525                                bool is_artificial);
526
527     clang::DeclContext *
528     GetDeclContextForType () const;
529
530     
531     bool
532     SetDefaultAccessForRecordFields (int default_accessibility,
533                                      int *assigned_accessibilities,
534                                      size_t num_assigned_accessibilities);
535     
536     bool
537     SetHasExternalStorage (bool has_extern);
538     
539     
540     //------------------------------------------------------------------
541     // clang::TagType
542     //------------------------------------------------------------------
543     
544     bool
545     SetTagTypeKind (int kind) const;
546     
547     //------------------------------------------------------------------
548     // Tag Declarations
549     //------------------------------------------------------------------
550     bool
551     StartTagDeclarationDefinition ();
552     
553     bool
554     CompleteTagDeclarationDefinition ();
555     
556     //----------------------------------------------------------------------
557     // Modifying Enumeration types
558     //----------------------------------------------------------------------
559     bool
560     AddEnumerationValueToEnumerationType (const ClangASTType &enumerator_qual_type,
561                                           const Declaration &decl,
562                                           const char *name,
563                                           int64_t enum_value,
564                                           uint32_t enum_value_bit_size);
565     
566
567     
568     ClangASTType
569     GetEnumerationIntegerType () const;
570
571     
572     //------------------------------------------------------------------
573     // Pointers & References
574     //------------------------------------------------------------------
575
576     // Call this function using the class type when you want to make a
577     // member pointer type to pointee_type.
578     ClangASTType
579     CreateMemberPointerType (const ClangASTType &pointee_type) const;
580     
581     
582     // Converts "s" to a floating point value and place resulting floating
583     // point bytes in the "dst" buffer.
584     size_t
585     ConvertStringToFloatValue (const char *s,
586                                uint8_t *dst,
587                                size_t dst_size) const;
588     //----------------------------------------------------------------------
589     // Dumping types
590     //----------------------------------------------------------------------
591     void
592     DumpValue (ExecutionContext *exe_ctx,
593                Stream *s,
594                lldb::Format format,
595                const DataExtractor &data,
596                lldb::offset_t data_offset,
597                size_t data_byte_size,
598                uint32_t bitfield_bit_size,
599                uint32_t bitfield_bit_offset,
600                bool show_types,
601                bool show_summary,
602                bool verbose,
603                uint32_t depth);
604
605     bool
606     DumpTypeValue (Stream *s,
607                    lldb::Format format,
608                    const DataExtractor &data,
609                    lldb::offset_t data_offset,
610                    size_t data_byte_size,
611                    uint32_t bitfield_bit_size,
612                    uint32_t bitfield_bit_offset,
613                    ExecutionContextScope *exe_scope);
614     
615     void
616     DumpSummary (ExecutionContext *exe_ctx,
617                  Stream *s,
618                  const DataExtractor &data,
619                  lldb::offset_t data_offset,
620                  size_t data_byte_size);
621
622     void
623     DumpTypeDescription () const; // Dump to stdout
624
625     void
626     DumpTypeDescription (Stream *s) const;
627     
628     bool
629     GetValueAsScalar (const DataExtractor &data,
630                       lldb::offset_t data_offset,
631                       size_t data_byte_size,
632                       Scalar &value) const;
633
634     bool
635     SetValueFromScalar (const Scalar &value,
636                         Stream &strm);
637
638     bool
639     ReadFromMemory (ExecutionContext *exe_ctx,
640                     lldb::addr_t addr,
641                     AddressType address_type,
642                     DataExtractor &data);
643
644     bool
645     WriteToMemory (ExecutionContext *exe_ctx,
646                    lldb::addr_t addr,
647                    AddressType address_type,
648                    StreamString &new_value);
649
650     clang::EnumDecl *
651     GetAsEnumDecl () const;
652
653     
654     clang::RecordDecl *
655     GetAsRecordDecl () const;
656     
657     clang::CXXRecordDecl *
658     GetAsCXXRecordDecl () const;
659     
660     clang::ObjCInterfaceDecl *
661     GetAsObjCInterfaceDecl () const;
662
663     void
664     Clear()
665     {
666         m_type = NULL;
667         m_ast = NULL;
668     }
669
670     clang::QualType
671     GetQualType () const
672     {
673         if (m_type)
674             return clang::QualType::getFromOpaquePtr(m_type);
675         return clang::QualType();
676     }
677     clang::QualType
678     GetCanonicalQualType () const
679     {
680         if (m_type)
681             return clang::QualType::getFromOpaquePtr(m_type).getCanonicalType();
682         return clang::QualType();
683     }
684
685 private:
686     lldb::clang_type_t m_type;
687     clang::ASTContext *m_ast;
688     
689 };
690     
691 bool operator == (const ClangASTType &lhs, const ClangASTType &rhs);
692 bool operator != (const ClangASTType &lhs, const ClangASTType &rhs);
693
694     
695 } // namespace lldb_private
696
697 #endif // #ifndef liblldb_ClangASTType_h_