Home | History | Annotate | Download | only in Symbol
      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     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     bool
    159     IsVariadicFunctionType () const;
    160 
    161     bool
    162     IsFunctionPointerType () const;
    163 
    164     bool
    165     IsIntegerType (bool &is_signed) const;
    166 
    167     bool
    168     IsObjCClassType () const;
    169 
    170     bool
    171     IsObjCClassTypeAndHasIVars (bool check_superclass) const;
    172 
    173     bool
    174     IsObjCObjectOrInterfaceType () const;
    175 
    176     bool
    177     IsObjCObjectPointerType (ClangASTType *target_type = NULL);
    178 
    179     bool
    180     IsPolymorphicClass () const;
    181 
    182     bool
    183     IsPossibleCPlusPlusDynamicType (ClangASTType *target_type = NULL) const
    184     {
    185         return IsPossibleDynamicType (target_type, true, false);
    186     }
    187 
    188     bool
    189     IsPossibleDynamicType (ClangASTType *target_type, // Can pass NULL
    190                            bool check_cplusplus,
    191                            bool check_objc) const;
    192 
    193 
    194     bool
    195     IsPointerToScalarType () const;
    196 
    197     bool
    198     IsPointerType (ClangASTType *pointee_type = NULL) const;
    199 
    200     bool
    201     IsPointerOrReferenceType (ClangASTType *pointee_type = NULL) const;
    202 
    203     bool
    204     IsReferenceType (ClangASTType *pointee_type = NULL) const;
    205 
    206     bool
    207     IsScalarType () const;
    208 
    209     bool
    210     IsTypedefType () const;
    211 
    212     bool
    213     IsVoidType () const;
    214 
    215     bool
    216     GetCXXClassName (std::string &class_name) const;
    217 
    218     bool
    219     GetObjCClassName (std::string &class_name);
    220 
    221 
    222     //----------------------------------------------------------------------
    223     // Type Completion
    224     //----------------------------------------------------------------------
    225 
    226     bool
    227     GetCompleteType () const;
    228 
    229     //----------------------------------------------------------------------
    230     // AST related queries
    231     //----------------------------------------------------------------------
    232 
    233     size_t
    234     GetPointerByteSize () const;
    235 
    236     //----------------------------------------------------------------------
    237     // Accessors
    238     //----------------------------------------------------------------------
    239 
    240     clang::ASTContext *
    241     GetASTContext() const
    242     {
    243         return m_ast;
    244     }
    245 
    246     ConstString
    247     GetConstQualifiedTypeName () const;
    248 
    249     ConstString
    250     GetConstTypeName () const;
    251 
    252     std::string
    253     GetTypeName () const;
    254 
    255     uint32_t
    256     GetTypeInfo (ClangASTType *pointee_or_element_clang_type = NULL) const;
    257 
    258     lldb::LanguageType
    259     GetMinimumLanguage ();
    260 
    261     lldb::clang_type_t
    262     GetOpaqueQualType() const
    263     {
    264         return m_type;
    265     }
    266 
    267     lldb::TypeClass
    268     GetTypeClass () const;
    269 
    270     void
    271     SetClangType (clang::ASTContext *ast, lldb::clang_type_t type)
    272     {
    273         m_ast = ast;
    274         m_type = type;
    275     }
    276 
    277     void
    278     SetClangType (clang::ASTContext *ast, clang::QualType qual_type);
    279 
    280     unsigned
    281     GetTypeQualifiers() const;
    282 
    283     //----------------------------------------------------------------------
    284     // Creating related types
    285     //----------------------------------------------------------------------
    286 
    287     ClangASTType
    288     AddConstModifier () const;
    289 
    290     ClangASTType
    291     AddRestrictModifier () const;
    292 
    293     ClangASTType
    294     AddVolatileModifier () const;
    295 
    296     // Using the current type, create a new typedef to that type using "typedef_name"
    297     // as the name and "decl_ctx" as the decl context.
    298     ClangASTType
    299     CreateTypedefType (const char *typedef_name,
    300                        clang::DeclContext *decl_ctx) const;
    301 
    302     ClangASTType
    303     GetArrayElementType (uint64_t& stride) const;
    304 
    305     ClangASTType
    306     GetCanonicalType () const;
    307 
    308     ClangASTType
    309     GetFullyUnqualifiedType () const;
    310 
    311     // Returns -1 if this isn't a function of if the fucntion doesn't have a prototype
    312     // Returns a value >= 0 if there is a prototype.
    313     int
    314     GetFunctionArgumentCount () const;
    315 
    316     ClangASTType
    317     GetFunctionArgumentTypeAtIndex (size_t idx);
    318 
    319     ClangASTType
    320     GetFunctionReturnType () const;
    321 
    322     ClangASTType
    323     GetLValueReferenceType () const;
    324 
    325     ClangASTType
    326     GetNonReferenceType () const;
    327 
    328     ClangASTType
    329     GetPointeeType () const;
    330 
    331     ClangASTType
    332     GetPointerType () const;
    333 
    334     ClangASTType
    335     GetRValueReferenceType () const;
    336 
    337     // If the current object represents a typedef type, get the underlying type
    338     ClangASTType
    339     GetTypedefedType () const;
    340 
    341     ClangASTType
    342     RemoveFastQualifiers () const;
    343 
    344     //----------------------------------------------------------------------
    345     // Create related types using the current type's AST
    346     //----------------------------------------------------------------------
    347     ClangASTType
    348     GetBasicTypeFromAST (lldb::BasicType basic_type) const;
    349 
    350     //----------------------------------------------------------------------
    351     // Exploring the type
    352     //----------------------------------------------------------------------
    353 
    354     uint64_t
    355     GetByteSize () const;
    356 
    357     uint64_t
    358     GetBitSize () const;
    359 
    360     lldb::Encoding
    361     GetEncoding (uint64_t &count) const;
    362 
    363     lldb::Format
    364     GetFormat () const;
    365 
    366     size_t
    367     GetTypeBitAlign () const;
    368 
    369     uint32_t
    370     GetNumChildren (bool omit_empty_base_classes) const;
    371 
    372     lldb::BasicType
    373     GetBasicTypeEnumeration () const;
    374 
    375     static lldb::BasicType
    376     GetBasicTypeEnumeration (const ConstString &name);
    377 
    378     uint32_t
    379     GetNumDirectBaseClasses () const;
    380 
    381     uint32_t
    382     GetNumVirtualBaseClasses () const;
    383 
    384     uint32_t
    385     GetNumFields () const;
    386 
    387     ClangASTType
    388     GetDirectBaseClassAtIndex (size_t idx,
    389                                uint32_t *bit_offset_ptr) const;
    390 
    391     ClangASTType
    392     GetVirtualBaseClassAtIndex (size_t idx,
    393                                 uint32_t *bit_offset_ptr) const;
    394 
    395     ClangASTType
    396     GetFieldAtIndex (size_t idx,
    397                      std::string& name,
    398                      uint64_t *bit_offset_ptr,
    399                      uint32_t *bitfield_bit_size_ptr,
    400                      bool *is_bitfield_ptr) const;
    401 
    402     uint32_t
    403     GetIndexOfFieldWithName (const char* name,
    404                              ClangASTType* field_clang_type = NULL,
    405                              uint64_t *bit_offset_ptr = NULL,
    406                              uint32_t *bitfield_bit_size_ptr = NULL,
    407                              bool *is_bitfield_ptr = NULL) const;
    408 
    409     uint32_t
    410     GetNumPointeeChildren () const;
    411 
    412     ClangASTType
    413     GetChildClangTypeAtIndex (ExecutionContext *exe_ctx,
    414                               const char *parent_name,
    415                               size_t idx,
    416                               bool transparent_pointers,
    417                               bool omit_empty_base_classes,
    418                               bool ignore_array_bounds,
    419                               std::string& child_name,
    420                               uint32_t &child_byte_size,
    421                               int32_t &child_byte_offset,
    422                               uint32_t &child_bitfield_bit_size,
    423                               uint32_t &child_bitfield_bit_offset,
    424                               bool &child_is_base_class,
    425                               bool &child_is_deref_of_parent) const;
    426 
    427     // Lookup a child given a name. This function will match base class names
    428     // and member member names in "clang_type" only, not descendants.
    429     uint32_t
    430     GetIndexOfChildWithName (const char *name,
    431                              bool omit_empty_base_classes) const;
    432 
    433     // Lookup a child member given a name. This function will match member names
    434     // only and will descend into "clang_type" children in search for the first
    435     // member in this class, or any base class that matches "name".
    436     // TODO: Return all matches for a given name by returning a vector<vector<uint32_t>>
    437     // so we catch all names that match a given child name, not just the first.
    438     size_t
    439     GetIndexOfChildMemberWithName (const char *name,
    440                                    bool omit_empty_base_classes,
    441                                    std::vector<uint32_t>& child_indexes) const;
    442 
    443     size_t
    444     GetNumTemplateArguments () const;
    445 
    446     ClangASTType
    447     GetTemplateArgument (size_t idx,
    448                          lldb::TemplateArgumentKind &kind) const;
    449 
    450 
    451     //----------------------------------------------------------------------
    452     // Modifying RecordType
    453     //----------------------------------------------------------------------
    454     clang::FieldDecl *
    455     AddFieldToRecordType (const char *name,
    456                           const ClangASTType &field_type,
    457                           lldb::AccessType access,
    458                           uint32_t bitfield_bit_size);
    459 
    460     void
    461     BuildIndirectFields ();
    462 
    463     clang::VarDecl *
    464     AddVariableToRecordType (const char *name,
    465                              const ClangASTType &var_type,
    466                              lldb::AccessType access);
    467 
    468     clang::CXXMethodDecl *
    469     AddMethodToCXXRecordType (const char *name,
    470                               const ClangASTType &method_type,
    471                               lldb::AccessType access,
    472                               bool is_virtual,
    473                               bool is_static,
    474                               bool is_inline,
    475                               bool is_explicit,
    476                               bool is_attr_used,
    477                               bool is_artificial);
    478 
    479     // C++ Base Classes
    480     clang::CXXBaseSpecifier *
    481     CreateBaseClassSpecifier (lldb::AccessType access,
    482                               bool is_virtual,
    483                               bool base_of_class);
    484 
    485     static void
    486     DeleteBaseClassSpecifiers (clang::CXXBaseSpecifier **base_classes,
    487                                unsigned num_base_classes);
    488 
    489     bool
    490     SetBaseClassesForClassType (clang::CXXBaseSpecifier const * const *base_classes,
    491                                 unsigned num_base_classes);
    492 
    493 
    494     bool
    495     SetObjCSuperClass (const ClangASTType &superclass_clang_type);
    496 
    497     bool
    498     AddObjCClassProperty (const char *property_name,
    499                           const ClangASTType &property_clang_type,
    500                           clang::ObjCIvarDecl *ivar_decl,
    501                           const char *property_setter_name,
    502                           const char *property_getter_name,
    503                           uint32_t property_attributes,
    504                           ClangASTMetadata *metadata);
    505 
    506     clang::ObjCMethodDecl *
    507     AddMethodToObjCObjectType (const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
    508                                const ClangASTType &method_clang_type,
    509                                lldb::AccessType access,
    510                                bool is_artificial);
    511 
    512     clang::DeclContext *
    513     GetDeclContextForType () const;
    514 
    515 
    516     bool
    517     SetDefaultAccessForRecordFields (int default_accessibility,
    518                                      int *assigned_accessibilities,
    519                                      size_t num_assigned_accessibilities);
    520 
    521     bool
    522     SetHasExternalStorage (bool has_extern);
    523 
    524 
    525     //------------------------------------------------------------------
    526     // clang::TagType
    527     //------------------------------------------------------------------
    528 
    529     bool
    530     SetTagTypeKind (int kind) const;
    531 
    532     //------------------------------------------------------------------
    533     // Tag Declarations
    534     //------------------------------------------------------------------
    535     bool
    536     StartTagDeclarationDefinition ();
    537 
    538     bool
    539     CompleteTagDeclarationDefinition ();
    540 
    541     //----------------------------------------------------------------------
    542     // Modifying Enumeration types
    543     //----------------------------------------------------------------------
    544     bool
    545     AddEnumerationValueToEnumerationType (const ClangASTType &enumerator_qual_type,
    546                                           const Declaration &decl,
    547                                           const char *name,
    548                                           int64_t enum_value,
    549                                           uint32_t enum_value_bit_size);
    550 
    551 
    552 
    553     ClangASTType
    554     GetEnumerationIntegerType () const;
    555 
    556 
    557     //------------------------------------------------------------------
    558     // Pointers & References
    559     //------------------------------------------------------------------
    560 
    561     // Call this function using the class type when you want to make a
    562     // member pointer type to pointee_type.
    563     ClangASTType
    564     CreateMemberPointerType (const ClangASTType &pointee_type) const;
    565 
    566 
    567     // Converts "s" to a floating point value and place resulting floating
    568     // point bytes in the "dst" buffer.
    569     size_t
    570     ConvertStringToFloatValue (const char *s,
    571                                uint8_t *dst,
    572                                size_t dst_size) const;
    573     //----------------------------------------------------------------------
    574     // Dumping types
    575     //----------------------------------------------------------------------
    576     void
    577     DumpValue (ExecutionContext *exe_ctx,
    578                Stream *s,
    579                lldb::Format format,
    580                const DataExtractor &data,
    581                lldb::offset_t data_offset,
    582                size_t data_byte_size,
    583                uint32_t bitfield_bit_size,
    584                uint32_t bitfield_bit_offset,
    585                bool show_types,
    586                bool show_summary,
    587                bool verbose,
    588                uint32_t depth);
    589 
    590     bool
    591     DumpTypeValue (Stream *s,
    592                    lldb::Format format,
    593                    const DataExtractor &data,
    594                    lldb::offset_t data_offset,
    595                    size_t data_byte_size,
    596                    uint32_t bitfield_bit_size,
    597                    uint32_t bitfield_bit_offset,
    598                    ExecutionContextScope *exe_scope);
    599 
    600     void
    601     DumpSummary (ExecutionContext *exe_ctx,
    602                  Stream *s,
    603                  const DataExtractor &data,
    604                  lldb::offset_t data_offset,
    605                  size_t data_byte_size);
    606 
    607     void
    608     DumpTypeDescription () const; // Dump to stdout
    609 
    610     void
    611     DumpTypeDescription (Stream *s) const;
    612 
    613     bool
    614     GetValueAsScalar (const DataExtractor &data,
    615                       lldb::offset_t data_offset,
    616                       size_t data_byte_size,
    617                       Scalar &value) const;
    618 
    619     bool
    620     SetValueFromScalar (const Scalar &value,
    621                         Stream &strm);
    622 
    623     bool
    624     ReadFromMemory (ExecutionContext *exe_ctx,
    625                     lldb::addr_t addr,
    626                     AddressType address_type,
    627                     DataExtractor &data);
    628 
    629     bool
    630     WriteToMemory (ExecutionContext *exe_ctx,
    631                    lldb::addr_t addr,
    632                    AddressType address_type,
    633                    StreamString &new_value);
    634 
    635 
    636     clang::RecordDecl *
    637     GetAsRecordDecl () const;
    638 
    639     clang::CXXRecordDecl *
    640     GetAsCXXRecordDecl () const;
    641 
    642     clang::ObjCInterfaceDecl *
    643     GetAsObjCInterfaceDecl () const;
    644 
    645     void
    646     Clear()
    647     {
    648         m_type = NULL;
    649         m_ast = NULL;
    650     }
    651 
    652     clang::QualType
    653     GetQualType () const
    654     {
    655         if (m_type)
    656             return clang::QualType::getFromOpaquePtr(m_type);
    657         return clang::QualType();
    658     }
    659     clang::QualType
    660     GetCanonicalQualType () const
    661     {
    662         if (m_type)
    663             return clang::QualType::getFromOpaquePtr(m_type).getCanonicalType();
    664         return clang::QualType();
    665     }
    666 
    667 private:
    668     lldb::clang_type_t m_type;
    669     clang::ASTContext *m_ast;
    670 
    671 };
    672 
    673 bool operator == (const ClangASTType &lhs, const ClangASTType &rhs);
    674 bool operator != (const ClangASTType &lhs, const ClangASTType &rhs);
    675 
    676 
    677 } // namespace lldb_private
    678 
    679 #endif // #ifndef liblldb_ClangASTType_h_
    680