1 //===-- ClangASTType.cpp ----------------------------------------*- 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 #include "lldb/lldb-python.h"
11 
12 #include "lldb/Symbol/ClangASTType.h"
13 
14 #include "clang/AST/ASTConsumer.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.h"
17 #include "clang/AST/CXXInheritance.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclObjC.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/DeclTemplate.h"
23 #include "clang/AST/RecordLayout.h"
24 #include "clang/AST/Type.h"
25 
26 #include "clang/Basic/Builtins.h"
27 #include "clang/Basic/IdentifierTable.h"
28 #include "clang/Basic/LangOptions.h"
29 #include "clang/Basic/SourceManager.h"
30 #include "clang/Basic/TargetInfo.h"
31 
32 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/Support/raw_ostream.h"
34 
35 #include "lldb/Core/ConstString.h"
36 #include "lldb/Core/DataBufferHeap.h"
37 #include "lldb/Core/DataExtractor.h"
38 #include "lldb/Core/Debugger.h"
39 #include "lldb/Core/Scalar.h"
40 #include "lldb/Core/Stream.h"
41 #include "lldb/Core/StreamString.h"
42 #include "lldb/Symbol/ClangASTContext.h"
43 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
44 #include "lldb/Symbol/VerifyDecl.h"
45 #include "lldb/Target/ExecutionContext.h"
46 #include "lldb/Target/Process.h"
47 
48 #include <mutex>
49 
50 using namespace lldb;
51 using namespace lldb_private;
52 using namespace clang;
53 using namespace llvm;
54 
55 static bool
GetCompleteQualType(ASTContext * ast,QualType qual_type,bool allow_completion=true)56 GetCompleteQualType (ASTContext *ast, QualType qual_type, bool allow_completion = true)
57 {
58     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
59     switch (type_class)
60     {
61         case clang::Type::ConstantArray:
62         case clang::Type::IncompleteArray:
63         case clang::Type::VariableArray:
64         {
65             const ArrayType *array_type = dyn_cast<ArrayType>(qual_type.getTypePtr());
66 
67             if (array_type)
68                 return GetCompleteQualType (ast, array_type->getElementType(), allow_completion);
69         }
70             break;
71 
72         case clang::Type::Record:
73         case clang::Type::Enum:
74         {
75             const TagType *tag_type = dyn_cast<TagType>(qual_type.getTypePtr());
76             if (tag_type)
77             {
78                 TagDecl *tag_decl = tag_type->getDecl();
79                 if (tag_decl)
80                 {
81                     if (tag_decl->isCompleteDefinition())
82                         return true;
83 
84                     if (!allow_completion)
85                         return false;
86 
87                     if (tag_decl->hasExternalLexicalStorage())
88                     {
89                         if (ast)
90                         {
91                             ExternalASTSource *external_ast_source = ast->getExternalSource();
92                             if (external_ast_source)
93                             {
94                                 external_ast_source->CompleteType(tag_decl);
95                                 return !tag_type->isIncompleteType();
96                             }
97                         }
98                     }
99                     return false;
100                 }
101             }
102 
103         }
104             break;
105 
106         case clang::Type::ObjCObject:
107         case clang::Type::ObjCInterface:
108         {
109             const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type);
110             if (objc_class_type)
111             {
112                 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
113                 // We currently can't complete objective C types through the newly added ASTContext
114                 // because it only supports TagDecl objects right now...
115                 if (class_interface_decl)
116                 {
117                     if (class_interface_decl->getDefinition())
118                         return true;
119 
120                     if (!allow_completion)
121                         return false;
122 
123                     if (class_interface_decl->hasExternalLexicalStorage())
124                     {
125                         if (ast)
126                         {
127                             ExternalASTSource *external_ast_source = ast->getExternalSource();
128                             if (external_ast_source)
129                             {
130                                 external_ast_source->CompleteType (class_interface_decl);
131                                 return !objc_class_type->isIncompleteType();
132                             }
133                         }
134                     }
135                     return false;
136                 }
137             }
138         }
139             break;
140 
141         case clang::Type::Typedef:
142             return GetCompleteQualType (ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType(), allow_completion);
143 
144         case clang::Type::Elaborated:
145             return GetCompleteQualType (ast, cast<ElaboratedType>(qual_type)->getNamedType(), allow_completion);
146 
147         case clang::Type::Paren:
148             return GetCompleteQualType (ast, cast<ParenType>(qual_type)->desugar(), allow_completion);
149 
150         default:
151             break;
152     }
153 
154     return true;
155 }
156 
157 static ObjCIvarDecl::AccessControl
ConvertAccessTypeToObjCIvarAccessControl(AccessType access)158 ConvertAccessTypeToObjCIvarAccessControl (AccessType access)
159 {
160     switch (access)
161     {
162         case eAccessNone:      return ObjCIvarDecl::None;
163         case eAccessPublic:    return ObjCIvarDecl::Public;
164         case eAccessPrivate:   return ObjCIvarDecl::Private;
165         case eAccessProtected: return ObjCIvarDecl::Protected;
166         case eAccessPackage:   return ObjCIvarDecl::Package;
167     }
168     return ObjCIvarDecl::None;
169 }
170 
171 //----------------------------------------------------------------------
172 // Tests
173 //----------------------------------------------------------------------
174 
ClangASTType(clang::ASTContext * ast,clang::QualType qual_type)175 ClangASTType::ClangASTType (clang::ASTContext *ast,
176                             clang::QualType qual_type) :
177     m_type (qual_type.getAsOpaquePtr()),
178     m_ast (ast)
179 {
180 }
181 
~ClangASTType()182 ClangASTType::~ClangASTType()
183 {
184 }
185 
186 //----------------------------------------------------------------------
187 // Tests
188 //----------------------------------------------------------------------
189 
190 bool
IsAggregateType() const191 ClangASTType::IsAggregateType () const
192 {
193     if (!IsValid())
194         return false;
195 
196     QualType qual_type (GetCanonicalQualType());
197 
198     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
199     switch (type_class)
200     {
201         case clang::Type::IncompleteArray:
202         case clang::Type::VariableArray:
203         case clang::Type::ConstantArray:
204         case clang::Type::ExtVector:
205         case clang::Type::Vector:
206         case clang::Type::Record:
207         case clang::Type::ObjCObject:
208         case clang::Type::ObjCInterface:
209             return true;
210         case clang::Type::Elaborated:
211             return ClangASTType(m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).IsAggregateType();
212         case clang::Type::Typedef:
213             return ClangASTType(m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsAggregateType();
214         case clang::Type::Paren:
215             return ClangASTType(m_ast, cast<ParenType>(qual_type)->desugar()).IsAggregateType();
216         default:
217             break;
218     }
219     // The clang type does have a value
220     return false;
221 }
222 
223 bool
IsArrayType(ClangASTType * element_type_ptr,uint64_t * size,bool * is_incomplete) const224 ClangASTType::IsArrayType (ClangASTType *element_type_ptr,
225                            uint64_t *size,
226                            bool *is_incomplete) const
227 {
228     if (IsValid())
229     {
230         QualType qual_type (GetCanonicalQualType());
231 
232         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
233         switch (type_class)
234         {
235             default:
236                 break;
237 
238             case clang::Type::ConstantArray:
239                 if (element_type_ptr)
240                     element_type_ptr->SetClangType (m_ast, cast<ConstantArrayType>(qual_type)->getElementType());
241                 if (size)
242                     *size = cast<ConstantArrayType>(qual_type)->getSize().getLimitedValue(ULLONG_MAX);
243                 return true;
244 
245             case clang::Type::IncompleteArray:
246                 if (element_type_ptr)
247                     element_type_ptr->SetClangType (m_ast, cast<IncompleteArrayType>(qual_type)->getElementType());
248                 if (size)
249                     *size = 0;
250                 if (is_incomplete)
251                     *is_incomplete = true;
252                 return true;
253 
254             case clang::Type::VariableArray:
255                 if (element_type_ptr)
256                     element_type_ptr->SetClangType (m_ast, cast<VariableArrayType>(qual_type)->getElementType());
257                 if (size)
258                     *size = 0;
259                 return true;
260 
261             case clang::Type::DependentSizedArray:
262                 if (element_type_ptr)
263                     element_type_ptr->SetClangType (m_ast, cast<DependentSizedArrayType>(qual_type)->getElementType());
264                 if (size)
265                     *size = 0;
266                 return true;
267 
268             case clang::Type::Typedef:
269                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsArrayType (element_type_ptr,
270                                                                                                                        size,
271                                                                                                                        is_incomplete);
272             case clang::Type::Elaborated:
273                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).IsArrayType (element_type_ptr,
274                                                                                                           size,
275                                                                                                           is_incomplete);
276             case clang::Type::Paren:
277                 return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).IsArrayType (element_type_ptr,
278                                                                                                        size,
279                                                                                                        is_incomplete);
280         }
281     }
282     if (element_type_ptr)
283         element_type_ptr->Clear();
284     if (size)
285         *size = 0;
286     if (is_incomplete)
287         *is_incomplete = false;
288     return 0;
289 }
290 
291 
292 bool
IsCharType() const293 ClangASTType::IsCharType () const
294 {
295     if (!IsValid())
296         return false;
297     return GetQualType().getUnqualifiedType()->isCharType();
298 }
299 
300 
301 bool
IsCompleteType() const302 ClangASTType::IsCompleteType () const
303 {
304     if (!IsValid())
305         return false;
306     const bool allow_completion = false;
307     return GetCompleteQualType (m_ast, GetQualType(), allow_completion);
308 }
309 
310 bool
IsConst() const311 ClangASTType::IsConst() const
312 {
313     return GetQualType().isConstQualified();
314 }
315 
316 bool
IsCStringType(uint32_t & length) const317 ClangASTType::IsCStringType (uint32_t &length) const
318 {
319     ClangASTType pointee_or_element_clang_type;
320     length = 0;
321     Flags type_flags (GetTypeInfo (&pointee_or_element_clang_type));
322 
323     if (!pointee_or_element_clang_type.IsValid())
324         return false;
325 
326     if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer))
327     {
328         if (pointee_or_element_clang_type.IsCharType())
329         {
330             if (type_flags.Test (eTypeIsArray))
331             {
332                 // We know the size of the array and it could be a C string
333                 // since it is an array of characters
334                 length = cast<ConstantArrayType>(GetCanonicalQualType().getTypePtr())->getSize().getLimitedValue();
335             }
336             return true;
337 
338         }
339     }
340     return false;
341 }
342 
343 bool
IsFunctionType(bool * is_variadic_ptr) const344 ClangASTType::IsFunctionType (bool *is_variadic_ptr) const
345 {
346     if (IsValid())
347     {
348         QualType qual_type (GetCanonicalQualType());
349 
350         if (qual_type->isFunctionType())
351         {
352             if (is_variadic_ptr)
353             {
354                 const clang::FunctionProtoType *function_proto_type = llvm::dyn_cast<clang::FunctionProtoType>(qual_type.getTypePtr());
355                 if (function_proto_type)
356                     *is_variadic_ptr = function_proto_type->isVariadic();
357                 else
358                     *is_variadic_ptr = false;
359             }
360             return true;
361         }
362 
363         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
364         switch (type_class)
365         {
366             default:
367                 break;
368             case clang::Type::Typedef:
369                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsFunctionType();
370             case clang::Type::Elaborated:
371                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).IsFunctionType();
372             case clang::Type::Paren:
373                 return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).IsFunctionType();
374 
375             case clang::Type::LValueReference:
376             case clang::Type::RValueReference:
377                 {
378                     const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
379                     if (reference_type)
380                         return ClangASTType (m_ast, reference_type->getPointeeType()).IsFunctionType();
381                 }
382                 break;
383         }
384     }
385     return false;
386 }
387 
388 
389 bool
IsFunctionPointerType() const390 ClangASTType::IsFunctionPointerType () const
391 {
392     if (IsValid())
393     {
394         QualType qual_type (GetCanonicalQualType());
395 
396         if (qual_type->isFunctionPointerType())
397             return true;
398 
399         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
400         switch (type_class)
401         {
402         default:
403             break;
404         case clang::Type::Typedef:
405             return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsFunctionPointerType();
406         case clang::Type::Elaborated:
407             return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).IsFunctionPointerType();
408         case clang::Type::Paren:
409             return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).IsFunctionPointerType();
410 
411         case clang::Type::LValueReference:
412         case clang::Type::RValueReference:
413             {
414                 const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
415                 if (reference_type)
416                     return ClangASTType (m_ast, reference_type->getPointeeType()).IsFunctionPointerType();
417             }
418             break;
419         }
420     }
421     return false;
422 
423 }
424 
425 bool
IsIntegerType(bool & is_signed) const426 ClangASTType::IsIntegerType (bool &is_signed) const
427 {
428     if (!IsValid())
429         return false;
430 
431     QualType qual_type (GetCanonicalQualType());
432     const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
433 
434     if (builtin_type)
435     {
436         if (builtin_type->isInteger())
437         {
438             is_signed = builtin_type->isSignedInteger();
439             return true;
440         }
441     }
442 
443     return false;
444 }
445 
446 bool
IsPointerType(ClangASTType * pointee_type) const447 ClangASTType::IsPointerType (ClangASTType *pointee_type) const
448 {
449     if (IsValid())
450     {
451         QualType qual_type (GetCanonicalQualType());
452         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
453         switch (type_class)
454         {
455             case clang::Type::Builtin:
456                 switch (cast<clang::BuiltinType>(qual_type)->getKind())
457                 {
458                     default:
459                         break;
460                     case clang::BuiltinType::ObjCId:
461                     case clang::BuiltinType::ObjCClass:
462                         return true;
463                 }
464                 return false;
465             case clang::Type::ObjCObjectPointer:
466                 if (pointee_type)
467                     pointee_type->SetClangType (m_ast, cast<ObjCObjectPointerType>(qual_type)->getPointeeType());
468                 return true;
469             case clang::Type::BlockPointer:
470                 if (pointee_type)
471                     pointee_type->SetClangType (m_ast, cast<BlockPointerType>(qual_type)->getPointeeType());
472                 return true;
473             case clang::Type::Pointer:
474                 if (pointee_type)
475                     pointee_type->SetClangType (m_ast, cast<PointerType>(qual_type)->getPointeeType());
476                 return true;
477             case clang::Type::MemberPointer:
478                 if (pointee_type)
479                     pointee_type->SetClangType (m_ast, cast<MemberPointerType>(qual_type)->getPointeeType());
480                 return true;
481             case clang::Type::Typedef:
482                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsPointerType(pointee_type);
483             case clang::Type::Elaborated:
484                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).IsPointerType(pointee_type);
485             case clang::Type::Paren:
486                 return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).IsPointerType(pointee_type);
487             default:
488                 break;
489         }
490     }
491     if (pointee_type)
492         pointee_type->Clear();
493     return false;
494 }
495 
496 
497 bool
IsPointerOrReferenceType(ClangASTType * pointee_type) const498 ClangASTType::IsPointerOrReferenceType (ClangASTType *pointee_type) const
499 {
500     if (IsValid())
501     {
502         QualType qual_type (GetCanonicalQualType());
503         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
504         switch (type_class)
505         {
506             case clang::Type::Builtin:
507                 switch (cast<clang::BuiltinType>(qual_type)->getKind())
508             {
509                 default:
510                     break;
511                 case clang::BuiltinType::ObjCId:
512                 case clang::BuiltinType::ObjCClass:
513                     return true;
514             }
515                 return false;
516             case clang::Type::ObjCObjectPointer:
517                 if (pointee_type)
518                     pointee_type->SetClangType(m_ast, cast<ObjCObjectPointerType>(qual_type)->getPointeeType());
519                 return true;
520             case clang::Type::BlockPointer:
521                 if (pointee_type)
522                     pointee_type->SetClangType(m_ast, cast<BlockPointerType>(qual_type)->getPointeeType());
523                 return true;
524             case clang::Type::Pointer:
525                 if (pointee_type)
526                     pointee_type->SetClangType(m_ast, cast<PointerType>(qual_type)->getPointeeType());
527                 return true;
528             case clang::Type::MemberPointer:
529                 if (pointee_type)
530                     pointee_type->SetClangType(m_ast, cast<MemberPointerType>(qual_type)->getPointeeType());
531                 return true;
532             case clang::Type::LValueReference:
533                 if (pointee_type)
534                     pointee_type->SetClangType(m_ast, cast<LValueReferenceType>(qual_type)->desugar());
535                 return true;
536             case clang::Type::RValueReference:
537                 if (pointee_type)
538                     pointee_type->SetClangType(m_ast, cast<LValueReferenceType>(qual_type)->desugar());
539                 return true;
540             case clang::Type::Typedef:
541                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsPointerOrReferenceType(pointee_type);
542             case clang::Type::Elaborated:
543                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).IsPointerOrReferenceType(pointee_type);
544             case clang::Type::Paren:
545                 return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).IsPointerOrReferenceType(pointee_type);
546             default:
547                 break;
548         }
549     }
550     if (pointee_type)
551         pointee_type->Clear();
552     return false;
553 }
554 
555 
556 bool
IsReferenceType(ClangASTType * pointee_type) const557 ClangASTType::IsReferenceType (ClangASTType *pointee_type) const
558 {
559     if (IsValid())
560     {
561         QualType qual_type (GetCanonicalQualType());
562         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
563 
564         switch (type_class)
565         {
566             case clang::Type::LValueReference:
567                 if (pointee_type)
568                     pointee_type->SetClangType(m_ast, cast<LValueReferenceType>(qual_type)->desugar());
569                 return true;
570             case clang::Type::RValueReference:
571                 if (pointee_type)
572                     pointee_type->SetClangType(m_ast, cast<LValueReferenceType>(qual_type)->desugar());
573                 return true;
574             case clang::Type::Typedef:
575                 return ClangASTType(m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsReferenceType(pointee_type);
576             case clang::Type::Elaborated:
577                 return ClangASTType(m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).IsReferenceType(pointee_type);
578             case clang::Type::Paren:
579                 return ClangASTType(m_ast, cast<clang::ParenType>(qual_type)->desugar()).IsReferenceType(pointee_type);
580 
581             default:
582                 break;
583         }
584     }
585     if (pointee_type)
586         pointee_type->Clear();
587     return false;
588 }
589 
590 bool
IsFloatingPointType(uint32_t & count,bool & is_complex) const591 ClangASTType::IsFloatingPointType (uint32_t &count, bool &is_complex) const
592 {
593     if (IsValid())
594     {
595         QualType qual_type (GetCanonicalQualType());
596 
597         if (const BuiltinType *BT = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal()))
598         {
599             clang::BuiltinType::Kind kind = BT->getKind();
600             if (kind >= BuiltinType::Float && kind <= BuiltinType::LongDouble)
601             {
602                 count = 1;
603                 is_complex = false;
604                 return true;
605             }
606         }
607         else if (const ComplexType *CT = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal()))
608         {
609             if (ClangASTType (m_ast, CT->getElementType()).IsFloatingPointType (count, is_complex))
610             {
611                 count = 2;
612                 is_complex = true;
613                 return true;
614             }
615         }
616         else if (const VectorType *VT = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal()))
617         {
618             if (ClangASTType (m_ast, VT->getElementType()).IsFloatingPointType (count, is_complex))
619             {
620                 count = VT->getNumElements();
621                 is_complex = false;
622                 return true;
623             }
624         }
625     }
626     count = 0;
627     is_complex = false;
628     return false;
629 }
630 
631 
632 bool
IsDefined() const633 ClangASTType::IsDefined() const
634 {
635     if (!IsValid())
636         return false;
637 
638     QualType qual_type(GetQualType());
639     const TagType *tag_type = dyn_cast<TagType>(qual_type.getTypePtr());
640     if (tag_type)
641     {
642         TagDecl *tag_decl = tag_type->getDecl();
643         if (tag_decl)
644             return tag_decl->isCompleteDefinition();
645         return false;
646     }
647     else
648     {
649         const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type);
650         if (objc_class_type)
651         {
652             ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
653             if (class_interface_decl)
654                 return class_interface_decl->getDefinition() != NULL;
655             return false;
656         }
657     }
658     return true;
659 }
660 
661 bool
IsObjCClassType() const662 ClangASTType::IsObjCClassType () const
663 {
664     if (IsValid())
665     {
666         QualType qual_type (GetCanonicalQualType());
667 
668         const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
669 
670         if (obj_pointer_type)
671             return obj_pointer_type->isObjCClassType();
672     }
673     return false;
674 }
675 
676 bool
IsObjCObjectOrInterfaceType() const677 ClangASTType::IsObjCObjectOrInterfaceType () const
678 {
679     if (IsValid())
680         return GetCanonicalQualType()->isObjCObjectOrInterfaceType();
681     return false;
682 }
683 
684 bool
IsPolymorphicClass() const685 ClangASTType::IsPolymorphicClass () const
686 {
687     if (IsValid())
688     {
689         QualType qual_type(GetCanonicalQualType());
690         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
691         switch (type_class)
692         {
693             case clang::Type::Record:
694                 if (GetCompleteType())
695                 {
696                     const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
697                     const RecordDecl *record_decl = record_type->getDecl();
698                     if (record_decl)
699                     {
700                         const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
701                         if (cxx_record_decl)
702                             return cxx_record_decl->isPolymorphic();
703                     }
704                 }
705                 break;
706 
707             default:
708                 break;
709         }
710     }
711     return false;
712 }
713 
714 bool
IsPossibleDynamicType(ClangASTType * dynamic_pointee_type,bool check_cplusplus,bool check_objc) const715 ClangASTType::IsPossibleDynamicType (ClangASTType *dynamic_pointee_type,
716                                      bool check_cplusplus,
717                                      bool check_objc) const
718 {
719     QualType pointee_qual_type;
720     if (m_type)
721     {
722         QualType qual_type (GetCanonicalQualType());
723         bool success = false;
724         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
725         switch (type_class)
726         {
727             case clang::Type::Builtin:
728                 if (check_objc && cast<BuiltinType>(qual_type)->getKind() == BuiltinType::ObjCId)
729                 {
730                     if (dynamic_pointee_type)
731                         dynamic_pointee_type->SetClangType(m_ast, m_type);
732                     return true;
733                 }
734                 break;
735 
736             case clang::Type::ObjCObjectPointer:
737                 if (check_objc)
738                 {
739                     if (dynamic_pointee_type)
740                         dynamic_pointee_type->SetClangType(m_ast, cast<ObjCObjectPointerType>(qual_type)->getPointeeType());
741                     return true;
742                 }
743                 break;
744 
745             case clang::Type::Pointer:
746                 pointee_qual_type = cast<PointerType>(qual_type)->getPointeeType();
747                 success = true;
748                 break;
749 
750             case clang::Type::LValueReference:
751             case clang::Type::RValueReference:
752                 pointee_qual_type = cast<ReferenceType>(qual_type)->getPointeeType();
753                 success = true;
754                 break;
755 
756             case clang::Type::Typedef:
757                 return ClangASTType (m_ast,
758                                      cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).IsPossibleDynamicType (dynamic_pointee_type,
759                                                                                                                           check_cplusplus,
760                                                                                                                           check_objc);
761 
762             case clang::Type::Elaborated:
763                 return ClangASTType (m_ast,
764                                      cast<ElaboratedType>(qual_type)->getNamedType()).IsPossibleDynamicType (dynamic_pointee_type,
765                                                                                                              check_cplusplus,
766                                                                                                              check_objc);
767 
768             case clang::Type::Paren:
769                 return ClangASTType (m_ast,
770                                      cast<ParenType>(qual_type)->desugar()).IsPossibleDynamicType (dynamic_pointee_type,
771                                                                                                    check_cplusplus,
772                                                                                                    check_objc);
773             default:
774                 break;
775         }
776 
777         if (success)
778         {
779             // Check to make sure what we are pointing too is a possible dynamic C++ type
780             // We currently accept any "void *" (in case we have a class that has been
781             // watered down to an opaque pointer) and virtual C++ classes.
782             const clang::Type::TypeClass pointee_type_class = pointee_qual_type.getCanonicalType()->getTypeClass();
783             switch (pointee_type_class)
784             {
785                 case clang::Type::Builtin:
786                     switch (cast<BuiltinType>(pointee_qual_type)->getKind())
787                 {
788                     case BuiltinType::UnknownAny:
789                     case BuiltinType::Void:
790                         if (dynamic_pointee_type)
791                             dynamic_pointee_type->SetClangType(m_ast, pointee_qual_type);
792                         return true;
793 
794                     case BuiltinType::NullPtr:
795                     case BuiltinType::Bool:
796                     case BuiltinType::Char_U:
797                     case BuiltinType::UChar:
798                     case BuiltinType::WChar_U:
799                     case BuiltinType::Char16:
800                     case BuiltinType::Char32:
801                     case BuiltinType::UShort:
802                     case BuiltinType::UInt:
803                     case BuiltinType::ULong:
804                     case BuiltinType::ULongLong:
805                     case BuiltinType::UInt128:
806                     case BuiltinType::Char_S:
807                     case BuiltinType::SChar:
808                     case BuiltinType::WChar_S:
809                     case BuiltinType::Short:
810                     case BuiltinType::Int:
811                     case BuiltinType::Long:
812                     case BuiltinType::LongLong:
813                     case BuiltinType::Int128:
814                     case BuiltinType::Float:
815                     case BuiltinType::Double:
816                     case BuiltinType::LongDouble:
817                     case BuiltinType::Dependent:
818                     case BuiltinType::Overload:
819                     case BuiltinType::ObjCId:
820                     case BuiltinType::ObjCClass:
821                     case BuiltinType::ObjCSel:
822                     case BuiltinType::BoundMember:
823                     case BuiltinType::Half:
824                     case BuiltinType::ARCUnbridgedCast:
825                     case BuiltinType::PseudoObject:
826                     case BuiltinType::BuiltinFn:
827                     case BuiltinType::OCLEvent:
828                     case BuiltinType::OCLImage1d:
829                     case BuiltinType::OCLImage1dArray:
830                     case BuiltinType::OCLImage1dBuffer:
831                     case BuiltinType::OCLImage2d:
832                     case BuiltinType::OCLImage2dArray:
833                     case BuiltinType::OCLImage3d:
834                     case BuiltinType::OCLSampler:
835                         break;
836                 }
837                     break;
838 
839                 case clang::Type::Record:
840                     if (check_cplusplus)
841                     {
842                         CXXRecordDecl *cxx_record_decl = pointee_qual_type->getAsCXXRecordDecl();
843                         if (cxx_record_decl)
844                         {
845                             bool is_complete = cxx_record_decl->isCompleteDefinition();
846 
847                             if (is_complete)
848                                 success = cxx_record_decl->isDynamicClass();
849                             else
850                             {
851                                 ClangASTMetadata *metadata = ClangASTContext::GetMetadata (m_ast, cxx_record_decl);
852                                 if (metadata)
853                                     success = metadata->GetIsDynamicCXXType();
854                                 else
855                                 {
856                                     is_complete = ClangASTType(m_ast, pointee_qual_type).GetCompleteType();
857                                     if (is_complete)
858                                         success = cxx_record_decl->isDynamicClass();
859                                     else
860                                         success = false;
861                                 }
862                             }
863 
864                             if (success)
865                             {
866                                 if (dynamic_pointee_type)
867                                     dynamic_pointee_type->SetClangType(m_ast, pointee_qual_type);
868                                 return true;
869                             }
870                         }
871                     }
872                     break;
873 
874                 case clang::Type::ObjCObject:
875                 case clang::Type::ObjCInterface:
876                     if (check_objc)
877                     {
878                         if (dynamic_pointee_type)
879                             dynamic_pointee_type->SetClangType(m_ast, pointee_qual_type);
880                         return true;
881                     }
882                     break;
883 
884                 default:
885                     break;
886             }
887         }
888     }
889     if (dynamic_pointee_type)
890         dynamic_pointee_type->Clear();
891     return false;
892 }
893 
894 
895 bool
IsScalarType() const896 ClangASTType::IsScalarType () const
897 {
898     if (!IsValid())
899         return false;
900 
901     return (GetTypeInfo (NULL) & eTypeIsScalar) != 0;
902 }
903 
904 bool
IsTypedefType() const905 ClangASTType::IsTypedefType () const
906 {
907     if (!IsValid())
908         return false;
909     return GetQualType()->getTypeClass() == clang::Type::Typedef;
910 }
911 
912 bool
IsVoidType() const913 ClangASTType::IsVoidType () const
914 {
915     if (!IsValid())
916         return false;
917     return GetCanonicalQualType()->isVoidType();
918 }
919 
920 bool
IsPointerToScalarType() const921 ClangASTType::IsPointerToScalarType () const
922 {
923     if (!IsValid())
924         return false;
925 
926     return IsPointerType() && GetPointeeType().IsScalarType();
927 }
928 
929 bool
IsArrayOfScalarType() const930 ClangASTType::IsArrayOfScalarType () const
931 {
932     ClangASTType element_type;
933     if (IsArrayType(&element_type, NULL, NULL))
934         return element_type.IsScalarType();
935     return false;
936 }
937 
938 
939 bool
GetCXXClassName(std::string & class_name) const940 ClangASTType::GetCXXClassName (std::string &class_name) const
941 {
942     if (IsValid())
943     {
944         QualType qual_type (GetCanonicalQualType());
945 
946         CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
947         if (cxx_record_decl)
948         {
949             class_name.assign (cxx_record_decl->getIdentifier()->getNameStart());
950             return true;
951         }
952     }
953     class_name.clear();
954     return false;
955 }
956 
957 
958 bool
IsCXXClassType() const959 ClangASTType::IsCXXClassType () const
960 {
961     if (!IsValid())
962         return false;
963 
964     QualType qual_type (GetCanonicalQualType());
965     if (qual_type->getAsCXXRecordDecl() != NULL)
966         return true;
967     return false;
968 }
969 
970 bool
IsBeingDefined() const971 ClangASTType::IsBeingDefined () const
972 {
973     if (!IsValid())
974         return false;
975     QualType qual_type (GetCanonicalQualType());
976     const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type);
977     if (tag_type)
978         return tag_type->isBeingDefined();
979     return false;
980 }
981 
982 bool
IsObjCObjectPointerType(ClangASTType * class_type_ptr)983 ClangASTType::IsObjCObjectPointerType (ClangASTType *class_type_ptr)
984 {
985     if (!IsValid())
986         return false;
987 
988     QualType qual_type (GetCanonicalQualType());
989 
990     if (qual_type->isObjCObjectPointerType())
991     {
992         if (class_type_ptr)
993         {
994             if (!qual_type->isObjCClassType() &&
995                 !qual_type->isObjCIdType())
996             {
997                 const ObjCObjectPointerType *obj_pointer_type = dyn_cast<ObjCObjectPointerType>(qual_type);
998                 if (obj_pointer_type == NULL)
999                     class_type_ptr->Clear();
1000                 else
1001                     class_type_ptr->SetClangType (m_ast, QualType(obj_pointer_type->getInterfaceType(), 0));
1002             }
1003         }
1004         return true;
1005     }
1006     if (class_type_ptr)
1007         class_type_ptr->Clear();
1008     return false;
1009 }
1010 
1011 bool
GetObjCClassName(std::string & class_name)1012 ClangASTType::GetObjCClassName (std::string &class_name)
1013 {
1014     if (!IsValid())
1015         return false;
1016 
1017     QualType qual_type (GetCanonicalQualType());
1018 
1019     const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(qual_type);
1020     if (object_type)
1021     {
1022         const ObjCInterfaceDecl *interface = object_type->getInterface();
1023         if (interface)
1024         {
1025             class_name = interface->getNameAsString();
1026             return true;
1027         }
1028     }
1029     return false;
1030 }
1031 
1032 
1033 //----------------------------------------------------------------------
1034 // Type Completion
1035 //----------------------------------------------------------------------
1036 
1037 bool
GetCompleteType() const1038 ClangASTType::GetCompleteType () const
1039 {
1040     if (!IsValid())
1041         return false;
1042     const bool allow_completion = true;
1043     return GetCompleteQualType (m_ast, GetQualType(), allow_completion);
1044 }
1045 
1046 //----------------------------------------------------------------------
1047 // AST related queries
1048 //----------------------------------------------------------------------
1049 size_t
GetPointerByteSize() const1050 ClangASTType::GetPointerByteSize () const
1051 {
1052     if (m_ast)
1053         return m_ast->getTypeSize(m_ast->VoidPtrTy) / 8;
1054     return 0;
1055 }
1056 
1057 ConstString
GetConstQualifiedTypeName() const1058 ClangASTType::GetConstQualifiedTypeName () const
1059 {
1060     return GetConstTypeName ();
1061 }
1062 
1063 ConstString
GetConstTypeName() const1064 ClangASTType::GetConstTypeName () const
1065 {
1066     if (IsValid())
1067     {
1068         std::string type_name (GetTypeName());
1069         if (!type_name.empty())
1070             return ConstString (type_name.c_str());
1071     }
1072     return ConstString("<invalid>");
1073 }
1074 
1075 std::string
GetTypeName() const1076 ClangASTType::GetTypeName () const
1077 {
1078     std::string type_name;
1079     if (IsValid())
1080     {
1081         PrintingPolicy printing_policy (m_ast->getPrintingPolicy());
1082         QualType qual_type(GetQualType());
1083         printing_policy.SuppressTagKeyword = true;
1084         printing_policy.LangOpts.WChar = true;
1085         const TypedefType *typedef_type = qual_type->getAs<TypedefType>();
1086         if (typedef_type)
1087         {
1088             const TypedefNameDecl *typedef_decl = typedef_type->getDecl();
1089             type_name = typedef_decl->getQualifiedNameAsString(printing_policy);
1090         }
1091         else
1092         {
1093             type_name = qual_type.getAsString(printing_policy);
1094         }
1095     }
1096     return type_name;
1097 }
1098 
1099 
1100 uint32_t
GetTypeInfo(ClangASTType * pointee_or_element_clang_type) const1101 ClangASTType::GetTypeInfo (ClangASTType *pointee_or_element_clang_type) const
1102 {
1103     if (!IsValid())
1104         return 0;
1105 
1106     if (pointee_or_element_clang_type)
1107         pointee_or_element_clang_type->Clear();
1108 
1109     QualType qual_type (GetQualType());
1110 
1111     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1112     switch (type_class)
1113     {
1114         case clang::Type::Builtin:
1115         {
1116             const BuiltinType *builtin_type = dyn_cast<BuiltinType>(qual_type->getCanonicalTypeInternal());
1117 
1118             uint32_t builtin_type_flags = eTypeIsBuiltIn | eTypeHasValue;
1119             switch (builtin_type->getKind())
1120             {
1121                 case clang::BuiltinType::ObjCId:
1122                 case clang::BuiltinType::ObjCClass:
1123                     if (pointee_or_element_clang_type)
1124                         pointee_or_element_clang_type->SetClangType(m_ast, m_ast->ObjCBuiltinClassTy);
1125                     builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
1126                     break;
1127 
1128                 case clang::BuiltinType::ObjCSel:
1129                     if (pointee_or_element_clang_type)
1130                         pointee_or_element_clang_type->SetClangType(m_ast, m_ast->CharTy);
1131                     builtin_type_flags |= eTypeIsPointer | eTypeIsObjC;
1132                     break;
1133 
1134                 case clang::BuiltinType::Bool:
1135                 case clang::BuiltinType::Char_U:
1136                 case clang::BuiltinType::UChar:
1137                 case clang::BuiltinType::WChar_U:
1138                 case clang::BuiltinType::Char16:
1139                 case clang::BuiltinType::Char32:
1140                 case clang::BuiltinType::UShort:
1141                 case clang::BuiltinType::UInt:
1142                 case clang::BuiltinType::ULong:
1143                 case clang::BuiltinType::ULongLong:
1144                 case clang::BuiltinType::UInt128:
1145                 case clang::BuiltinType::Char_S:
1146                 case clang::BuiltinType::SChar:
1147                 case clang::BuiltinType::WChar_S:
1148                 case clang::BuiltinType::Short:
1149                 case clang::BuiltinType::Int:
1150                 case clang::BuiltinType::Long:
1151                 case clang::BuiltinType::LongLong:
1152                 case clang::BuiltinType::Int128:
1153                 case clang::BuiltinType::Float:
1154                 case clang::BuiltinType::Double:
1155                 case clang::BuiltinType::LongDouble:
1156                     builtin_type_flags |= eTypeIsScalar;
1157                     if (builtin_type->isInteger())
1158                     {
1159                         builtin_type_flags |= eTypeIsInteger;
1160                         if (builtin_type->isSignedInteger())
1161                             builtin_type_flags |= eTypeIsSigned;
1162                     }
1163                     else if (builtin_type->isFloatingPoint())
1164                         builtin_type_flags |= eTypeIsFloat;
1165                     break;
1166                 default:
1167                     break;
1168             }
1169             return builtin_type_flags;
1170         }
1171 
1172         case clang::Type::BlockPointer:
1173             if (pointee_or_element_clang_type)
1174                 pointee_or_element_clang_type->SetClangType(m_ast, qual_type->getPointeeType());
1175             return eTypeIsPointer | eTypeHasChildren | eTypeIsBlock;
1176 
1177         case clang::Type::Complex:
1178         {
1179             uint32_t complex_type_flags = eTypeIsBuiltIn | eTypeHasValue | eTypeIsComplex;
1180             const ComplexType *complex_type = dyn_cast<ComplexType>(qual_type->getCanonicalTypeInternal());
1181             if (complex_type)
1182             {
1183                 QualType complex_element_type (complex_type->getElementType());
1184                 if (complex_element_type->isIntegerType())
1185                     complex_type_flags |= eTypeIsFloat;
1186                 else if (complex_element_type->isFloatingType())
1187                     complex_type_flags |= eTypeIsInteger;
1188             }
1189             return complex_type_flags;
1190         }
1191             break;
1192 
1193         case clang::Type::ConstantArray:
1194         case clang::Type::DependentSizedArray:
1195         case clang::Type::IncompleteArray:
1196         case clang::Type::VariableArray:
1197             if (pointee_or_element_clang_type)
1198                 pointee_or_element_clang_type->SetClangType(m_ast, cast<ArrayType>(qual_type.getTypePtr())->getElementType());
1199             return eTypeHasChildren | eTypeIsArray;
1200 
1201         case clang::Type::DependentName:                    return 0;
1202         case clang::Type::DependentSizedExtVector:          return eTypeHasChildren | eTypeIsVector;
1203         case clang::Type::DependentTemplateSpecialization:  return eTypeIsTemplate;
1204         case clang::Type::Decltype:                         return 0;
1205 
1206         case clang::Type::Enum:
1207             if (pointee_or_element_clang_type)
1208                 pointee_or_element_clang_type->SetClangType(m_ast, cast<EnumType>(qual_type)->getDecl()->getIntegerType());
1209             return eTypeIsEnumeration | eTypeHasValue;
1210 
1211         case clang::Type::Elaborated:
1212             return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetTypeInfo (pointee_or_element_clang_type);
1213         case clang::Type::Paren:
1214             return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetTypeInfo (pointee_or_element_clang_type);
1215 
1216         case clang::Type::FunctionProto:                    return eTypeIsFuncPrototype | eTypeHasValue;
1217         case clang::Type::FunctionNoProto:                  return eTypeIsFuncPrototype | eTypeHasValue;
1218         case clang::Type::InjectedClassName:                return 0;
1219 
1220         case clang::Type::LValueReference:
1221         case clang::Type::RValueReference:
1222             if (pointee_or_element_clang_type)
1223                 pointee_or_element_clang_type->SetClangType(m_ast, cast<ReferenceType>(qual_type.getTypePtr())->getPointeeType());
1224             return eTypeHasChildren | eTypeIsReference | eTypeHasValue;
1225 
1226         case clang::Type::MemberPointer:                    return eTypeIsPointer   | eTypeIsMember | eTypeHasValue;
1227 
1228         case clang::Type::ObjCObjectPointer:
1229             if (pointee_or_element_clang_type)
1230                 pointee_or_element_clang_type->SetClangType(m_ast, qual_type->getPointeeType());
1231             return eTypeHasChildren | eTypeIsObjC | eTypeIsClass | eTypeIsPointer | eTypeHasValue;
1232 
1233         case clang::Type::ObjCObject:                       return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
1234         case clang::Type::ObjCInterface:                    return eTypeHasChildren | eTypeIsObjC | eTypeIsClass;
1235 
1236         case clang::Type::Pointer:
1237             if (pointee_or_element_clang_type)
1238                 pointee_or_element_clang_type->SetClangType(m_ast, qual_type->getPointeeType());
1239             return eTypeHasChildren | eTypeIsPointer | eTypeHasValue;
1240 
1241         case clang::Type::Record:
1242             if (qual_type->getAsCXXRecordDecl())
1243                 return eTypeHasChildren | eTypeIsClass | eTypeIsCPlusPlus;
1244             else
1245                 return eTypeHasChildren | eTypeIsStructUnion;
1246             break;
1247         case clang::Type::SubstTemplateTypeParm:            return eTypeIsTemplate;
1248         case clang::Type::TemplateTypeParm:                 return eTypeIsTemplate;
1249         case clang::Type::TemplateSpecialization:           return eTypeIsTemplate;
1250 
1251         case clang::Type::Typedef:
1252             return eTypeIsTypedef | ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetTypeInfo (pointee_or_element_clang_type);
1253         case clang::Type::TypeOfExpr:                       return 0;
1254         case clang::Type::TypeOf:                           return 0;
1255         case clang::Type::UnresolvedUsing:                  return 0;
1256 
1257         case clang::Type::ExtVector:
1258         case clang::Type::Vector:
1259         {
1260             uint32_t vector_type_flags = eTypeHasChildren | eTypeIsVector;
1261             const VectorType *vector_type = dyn_cast<VectorType>(qual_type->getCanonicalTypeInternal());
1262             if (vector_type)
1263             {
1264                 if (vector_type->isIntegerType())
1265                     vector_type_flags |= eTypeIsFloat;
1266                 else if (vector_type->isFloatingType())
1267                     vector_type_flags |= eTypeIsInteger;
1268             }
1269             return vector_type_flags;
1270         }
1271         default:                                            return 0;
1272     }
1273     return 0;
1274 }
1275 
1276 
1277 
1278 lldb::LanguageType
GetMinimumLanguage()1279 ClangASTType::GetMinimumLanguage ()
1280 {
1281     if (!IsValid())
1282         return lldb::eLanguageTypeC;
1283 
1284     // If the type is a reference, then resolve it to what it refers to first:
1285     QualType qual_type (GetCanonicalQualType().getNonReferenceType());
1286     if (qual_type->isAnyPointerType())
1287     {
1288         if (qual_type->isObjCObjectPointerType())
1289             return lldb::eLanguageTypeObjC;
1290 
1291         QualType pointee_type (qual_type->getPointeeType());
1292         if (pointee_type->getPointeeCXXRecordDecl() != NULL)
1293             return lldb::eLanguageTypeC_plus_plus;
1294         if (pointee_type->isObjCObjectOrInterfaceType())
1295             return lldb::eLanguageTypeObjC;
1296         if (pointee_type->isObjCClassType())
1297             return lldb::eLanguageTypeObjC;
1298         if (pointee_type.getTypePtr() == m_ast->ObjCBuiltinIdTy.getTypePtr())
1299             return lldb::eLanguageTypeObjC;
1300     }
1301     else
1302     {
1303         if (qual_type->isObjCObjectOrInterfaceType())
1304             return lldb::eLanguageTypeObjC;
1305         if (qual_type->getAsCXXRecordDecl())
1306             return lldb::eLanguageTypeC_plus_plus;
1307         switch (qual_type->getTypeClass())
1308         {
1309             default:
1310                 break;
1311             case clang::Type::Builtin:
1312                 switch (cast<BuiltinType>(qual_type)->getKind())
1313             {
1314                 default:
1315                 case BuiltinType::Void:
1316                 case BuiltinType::Bool:
1317                 case BuiltinType::Char_U:
1318                 case BuiltinType::UChar:
1319                 case BuiltinType::WChar_U:
1320                 case BuiltinType::Char16:
1321                 case BuiltinType::Char32:
1322                 case BuiltinType::UShort:
1323                 case BuiltinType::UInt:
1324                 case BuiltinType::ULong:
1325                 case BuiltinType::ULongLong:
1326                 case BuiltinType::UInt128:
1327                 case BuiltinType::Char_S:
1328                 case BuiltinType::SChar:
1329                 case BuiltinType::WChar_S:
1330                 case BuiltinType::Short:
1331                 case BuiltinType::Int:
1332                 case BuiltinType::Long:
1333                 case BuiltinType::LongLong:
1334                 case BuiltinType::Int128:
1335                 case BuiltinType::Float:
1336                 case BuiltinType::Double:
1337                 case BuiltinType::LongDouble:
1338                     break;
1339 
1340                 case BuiltinType::NullPtr:
1341                     return eLanguageTypeC_plus_plus;
1342 
1343                 case BuiltinType::ObjCId:
1344                 case BuiltinType::ObjCClass:
1345                 case BuiltinType::ObjCSel:
1346                     return eLanguageTypeObjC;
1347 
1348                 case BuiltinType::Dependent:
1349                 case BuiltinType::Overload:
1350                 case BuiltinType::BoundMember:
1351                 case BuiltinType::UnknownAny:
1352                     break;
1353             }
1354                 break;
1355             case clang::Type::Typedef:
1356                 return ClangASTType(m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetMinimumLanguage();
1357         }
1358     }
1359     return lldb::eLanguageTypeC;
1360 }
1361 
1362 lldb::TypeClass
GetTypeClass() const1363 ClangASTType::GetTypeClass () const
1364 {
1365     if (!IsValid())
1366         return lldb::eTypeClassInvalid;
1367 
1368     QualType qual_type(GetQualType());
1369 
1370     switch (qual_type->getTypeClass())
1371     {
1372         case clang::Type::UnaryTransform:           break;
1373         case clang::Type::FunctionNoProto:          return lldb::eTypeClassFunction;
1374         case clang::Type::FunctionProto:            return lldb::eTypeClassFunction;
1375         case clang::Type::IncompleteArray:          return lldb::eTypeClassArray;
1376         case clang::Type::VariableArray:            return lldb::eTypeClassArray;
1377         case clang::Type::ConstantArray:            return lldb::eTypeClassArray;
1378         case clang::Type::DependentSizedArray:      return lldb::eTypeClassArray;
1379         case clang::Type::DependentSizedExtVector:  return lldb::eTypeClassVector;
1380         case clang::Type::ExtVector:                return lldb::eTypeClassVector;
1381         case clang::Type::Vector:                   return lldb::eTypeClassVector;
1382         case clang::Type::Builtin:                  return lldb::eTypeClassBuiltin;
1383         case clang::Type::ObjCObjectPointer:        return lldb::eTypeClassObjCObjectPointer;
1384         case clang::Type::BlockPointer:             return lldb::eTypeClassBlockPointer;
1385         case clang::Type::Pointer:                  return lldb::eTypeClassPointer;
1386         case clang::Type::LValueReference:          return lldb::eTypeClassReference;
1387         case clang::Type::RValueReference:          return lldb::eTypeClassReference;
1388         case clang::Type::MemberPointer:            return lldb::eTypeClassMemberPointer;
1389         case clang::Type::Complex:
1390             if (qual_type->isComplexType())
1391                 return lldb::eTypeClassComplexFloat;
1392             else
1393                 return lldb::eTypeClassComplexInteger;
1394         case clang::Type::ObjCObject:               return lldb::eTypeClassObjCObject;
1395         case clang::Type::ObjCInterface:            return lldb::eTypeClassObjCInterface;
1396         case clang::Type::Record:
1397             {
1398                 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
1399                 const RecordDecl *record_decl = record_type->getDecl();
1400                 if (record_decl->isUnion())
1401                     return lldb::eTypeClassUnion;
1402                 else if (record_decl->isStruct())
1403                     return lldb::eTypeClassStruct;
1404                 else
1405                     return lldb::eTypeClassClass;
1406             }
1407             break;
1408         case clang::Type::Enum:                     return lldb::eTypeClassEnumeration;
1409         case clang::Type::Typedef:                  return lldb::eTypeClassTypedef;
1410         case clang::Type::UnresolvedUsing:          break;
1411         case clang::Type::Paren:
1412             return ClangASTType(m_ast, cast<ParenType>(qual_type)->desugar()).GetTypeClass();
1413         case clang::Type::Elaborated:
1414             return ClangASTType(m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetTypeClass();
1415 
1416         case clang::Type::Attributed:               break;
1417         case clang::Type::TemplateTypeParm:         break;
1418         case clang::Type::SubstTemplateTypeParm:    break;
1419         case clang::Type::SubstTemplateTypeParmPack:break;
1420         case clang::Type::Auto:                     break;
1421         case clang::Type::InjectedClassName:        break;
1422         case clang::Type::DependentName:            break;
1423         case clang::Type::DependentTemplateSpecialization: break;
1424         case clang::Type::PackExpansion:            break;
1425 
1426         case clang::Type::TypeOfExpr:               break;
1427         case clang::Type::TypeOf:                   break;
1428         case clang::Type::Decltype:                 break;
1429         case clang::Type::TemplateSpecialization:   break;
1430         case clang::Type::Atomic:                   break;
1431     }
1432     // We don't know hot to display this type...
1433     return lldb::eTypeClassOther;
1434 
1435 }
1436 
1437 void
SetClangType(clang::ASTContext * ast,clang::QualType qual_type)1438 ClangASTType::SetClangType (clang::ASTContext *ast, clang::QualType qual_type)
1439 {
1440     m_ast = ast;
1441     m_type = qual_type.getAsOpaquePtr();
1442 }
1443 
1444 unsigned
GetTypeQualifiers() const1445 ClangASTType::GetTypeQualifiers() const
1446 {
1447     if (IsValid())
1448         return GetQualType().getQualifiers().getCVRQualifiers();
1449     return 0;
1450 }
1451 
1452 //----------------------------------------------------------------------
1453 // Creating related types
1454 //----------------------------------------------------------------------
1455 
1456 ClangASTType
AddConstModifier() const1457 ClangASTType::AddConstModifier () const
1458 {
1459     if (m_type)
1460     {
1461         QualType result(GetQualType());
1462         result.addConst();
1463         return ClangASTType (m_ast, result);
1464     }
1465     return ClangASTType();
1466 }
1467 
1468 ClangASTType
AddRestrictModifier() const1469 ClangASTType::AddRestrictModifier () const
1470 {
1471     if (m_type)
1472     {
1473         QualType result(GetQualType());
1474         result.getQualifiers().setRestrict (true);
1475         return ClangASTType (m_ast, result);
1476     }
1477     return ClangASTType();
1478 }
1479 
1480 ClangASTType
AddVolatileModifier() const1481 ClangASTType::AddVolatileModifier () const
1482 {
1483     if (m_type)
1484     {
1485         QualType result(GetQualType());
1486         result.getQualifiers().setVolatile (true);
1487         return ClangASTType (m_ast, result);
1488     }
1489     return ClangASTType();
1490 }
1491 
1492 ClangASTType
GetArrayElementType(uint64_t & stride) const1493 ClangASTType::GetArrayElementType (uint64_t& stride) const
1494 {
1495     if (IsValid())
1496     {
1497         QualType qual_type(GetCanonicalQualType());
1498 
1499         ClangASTType element_type (m_ast, qual_type.getTypePtr()->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
1500 
1501         // TODO: the real stride will be >= this value.. find the real one!
1502         stride = element_type.GetByteSize();
1503 
1504         return element_type;
1505 
1506     }
1507     return ClangASTType();
1508 }
1509 
1510 ClangASTType
GetCanonicalType() const1511 ClangASTType::GetCanonicalType () const
1512 {
1513     if (IsValid())
1514         return ClangASTType (m_ast, GetCanonicalQualType());
1515     return ClangASTType();
1516 }
1517 
1518 static QualType
GetFullyUnqualifiedType_Impl(ASTContext * ast,QualType qual_type)1519 GetFullyUnqualifiedType_Impl (ASTContext *ast, QualType qual_type)
1520 {
1521     if (qual_type->isPointerType())
1522         qual_type = ast->getPointerType(GetFullyUnqualifiedType_Impl(ast, qual_type->getPointeeType()));
1523     else
1524         qual_type = qual_type.getUnqualifiedType();
1525     qual_type.removeLocalConst();
1526     qual_type.removeLocalRestrict();
1527     qual_type.removeLocalVolatile();
1528     return qual_type;
1529 }
1530 
1531 ClangASTType
GetFullyUnqualifiedType() const1532 ClangASTType::GetFullyUnqualifiedType () const
1533 {
1534     if (IsValid())
1535         return ClangASTType(m_ast, GetFullyUnqualifiedType_Impl(m_ast, GetQualType()));
1536     return ClangASTType();
1537 }
1538 
1539 
1540 int
GetFunctionArgumentCount() const1541 ClangASTType::GetFunctionArgumentCount () const
1542 {
1543     if (IsValid())
1544     {
1545         const FunctionProtoType* func = dyn_cast<FunctionProtoType>(GetCanonicalQualType());
1546         if (func)
1547             return func->getNumArgs();
1548     }
1549     return -1;
1550 }
1551 
1552 ClangASTType
GetFunctionArgumentTypeAtIndex(size_t idx)1553 ClangASTType::GetFunctionArgumentTypeAtIndex (size_t idx)
1554 {
1555     if (IsValid())
1556     {
1557         const FunctionProtoType* func = dyn_cast<FunctionProtoType>(GetCanonicalQualType());
1558         if (func)
1559         {
1560             const uint32_t num_args = func->getNumArgs();
1561             if (idx < num_args)
1562                 return ClangASTType(m_ast, func->getArgType(idx));
1563         }
1564     }
1565     return ClangASTType();
1566 }
1567 
1568 ClangASTType
GetFunctionReturnType() const1569 ClangASTType::GetFunctionReturnType () const
1570 {
1571     if (IsValid())
1572     {
1573         QualType qual_type(GetCanonicalQualType());
1574         const FunctionProtoType* func = dyn_cast<FunctionProtoType>(qual_type.getTypePtr());
1575         if (func)
1576             return ClangASTType(m_ast, func->getResultType());
1577     }
1578     return ClangASTType();
1579 }
1580 
1581 
1582 ClangASTType
GetLValueReferenceType() const1583 ClangASTType::GetLValueReferenceType () const
1584 {
1585     if (IsValid())
1586     {
1587         return ClangASTType(m_ast, m_ast->getLValueReferenceType(GetQualType()));
1588     }
1589     return ClangASTType();
1590 }
1591 
1592 ClangASTType
GetRValueReferenceType() const1593 ClangASTType::GetRValueReferenceType () const
1594 {
1595     if (IsValid())
1596     {
1597         return ClangASTType(m_ast, m_ast->getRValueReferenceType(GetQualType()));
1598     }
1599     return ClangASTType();
1600 }
1601 
1602 ClangASTType
GetNonReferenceType() const1603 ClangASTType::GetNonReferenceType () const
1604 {
1605     if (IsValid())
1606         return ClangASTType(m_ast, GetQualType().getNonReferenceType());
1607     return ClangASTType();
1608 }
1609 
1610 ClangASTType
CreateTypedefType(const char * typedef_name,clang::DeclContext * decl_ctx) const1611 ClangASTType::CreateTypedefType (const char *typedef_name,
1612                                  clang::DeclContext *decl_ctx) const
1613 {
1614     if (IsValid() && typedef_name && typedef_name[0])
1615     {
1616         QualType qual_type (GetQualType());
1617         if (decl_ctx == NULL)
1618             decl_ctx = m_ast->getTranslationUnitDecl();
1619         TypedefDecl *decl = TypedefDecl::Create (*m_ast,
1620                                                  decl_ctx,
1621                                                  SourceLocation(),
1622                                                  SourceLocation(),
1623                                                  &m_ast->Idents.get(typedef_name),
1624                                                  m_ast->getTrivialTypeSourceInfo(qual_type));
1625 
1626         decl->setAccess(AS_public); // TODO respect proper access specifier
1627 
1628         // Get a uniqued QualType for the typedef decl type
1629         return ClangASTType (m_ast, m_ast->getTypedefType (decl));
1630     }
1631     return ClangASTType();
1632 
1633 }
1634 
1635 ClangASTType
GetPointeeType() const1636 ClangASTType::GetPointeeType () const
1637 {
1638     if (m_type)
1639     {
1640         QualType qual_type(GetQualType());
1641         return ClangASTType (m_ast, qual_type.getTypePtr()->getPointeeType());
1642     }
1643     return ClangASTType();
1644 }
1645 
1646 ClangASTType
GetPointerType() const1647 ClangASTType::GetPointerType () const
1648 {
1649     if (IsValid())
1650     {
1651         QualType qual_type (GetQualType());
1652 
1653         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
1654         switch (type_class)
1655         {
1656             case clang::Type::ObjCObject:
1657             case clang::Type::ObjCInterface:
1658                 return ClangASTType(m_ast, m_ast->getObjCObjectPointerType(qual_type).getAsOpaquePtr());
1659 
1660             default:
1661                 return ClangASTType(m_ast, m_ast->getPointerType(qual_type).getAsOpaquePtr());
1662         }
1663     }
1664     return ClangASTType();
1665 }
1666 
1667 ClangASTType
GetTypedefedType() const1668 ClangASTType::GetTypedefedType () const
1669 {
1670     if (IsValid())
1671     {
1672         const TypedefType *typedef_type = dyn_cast<TypedefType>(GetQualType());
1673         if (typedef_type)
1674             return ClangASTType (m_ast, typedef_type->getDecl()->getUnderlyingType());
1675     }
1676     return ClangASTType();
1677 }
1678 
1679 ClangASTType
RemoveFastQualifiers() const1680 ClangASTType::RemoveFastQualifiers () const
1681 {
1682     if (m_type)
1683     {
1684         QualType qual_type(GetQualType());
1685         qual_type.getQualifiers().removeFastQualifiers();
1686         return ClangASTType (m_ast, qual_type);
1687     }
1688     return ClangASTType();
1689 }
1690 
1691 
1692 //----------------------------------------------------------------------
1693 // Create related types using the current type's AST
1694 //----------------------------------------------------------------------
1695 
1696 ClangASTType
GetBasicTypeFromAST(lldb::BasicType basic_type) const1697 ClangASTType::GetBasicTypeFromAST (lldb::BasicType basic_type) const
1698 {
1699     if (IsValid())
1700         return ClangASTContext::GetBasicType(m_ast, basic_type);
1701     return ClangASTType();
1702 }
1703 //----------------------------------------------------------------------
1704 // Exploring the type
1705 //----------------------------------------------------------------------
1706 
1707 uint64_t
GetBitSize() const1708 ClangASTType::GetBitSize () const
1709 {
1710     if (GetCompleteType ())
1711     {
1712         QualType qual_type(GetCanonicalQualType());
1713         const uint32_t bit_size = m_ast->getTypeSize (qual_type);
1714         if (bit_size == 0)
1715         {
1716             if (qual_type->isIncompleteArrayType())
1717                 return m_ast->getTypeSize (qual_type->getArrayElementTypeNoTypeQual()->getCanonicalTypeUnqualified());
1718         }
1719         if (qual_type->isObjCObjectOrInterfaceType())
1720             return bit_size + m_ast->getTypeSize(m_ast->ObjCBuiltinClassTy);
1721         return bit_size;
1722     }
1723     return 0;
1724 }
1725 
1726 uint64_t
GetByteSize() const1727 ClangASTType::GetByteSize () const
1728 {
1729     return (GetBitSize () + 7) / 8;
1730 }
1731 
1732 size_t
GetTypeBitAlign() const1733 ClangASTType::GetTypeBitAlign () const
1734 {
1735     if (GetCompleteType ())
1736         return m_ast->getTypeAlign(GetQualType());
1737     return 0;
1738 }
1739 
1740 
1741 lldb::Encoding
GetEncoding(uint64_t & count) const1742 ClangASTType::GetEncoding (uint64_t &count) const
1743 {
1744     if (!IsValid())
1745         return lldb::eEncodingInvalid;
1746 
1747     count = 1;
1748     QualType qual_type(GetCanonicalQualType());
1749 
1750     switch (qual_type->getTypeClass())
1751     {
1752         case clang::Type::UnaryTransform:
1753             break;
1754 
1755         case clang::Type::FunctionNoProto:
1756         case clang::Type::FunctionProto:
1757             break;
1758 
1759         case clang::Type::IncompleteArray:
1760         case clang::Type::VariableArray:
1761             break;
1762 
1763         case clang::Type::ConstantArray:
1764             break;
1765 
1766         case clang::Type::ExtVector:
1767         case clang::Type::Vector:
1768             // TODO: Set this to more than one???
1769             break;
1770 
1771         case clang::Type::Builtin:
1772             switch (cast<BuiltinType>(qual_type)->getKind())
1773         {
1774             default: assert(0 && "Unknown builtin type!");
1775             case BuiltinType::Void:
1776                 break;
1777 
1778             case BuiltinType::Bool:
1779             case BuiltinType::Char_S:
1780             case BuiltinType::SChar:
1781             case BuiltinType::WChar_S:
1782             case BuiltinType::Char16:
1783             case BuiltinType::Char32:
1784             case BuiltinType::Short:
1785             case BuiltinType::Int:
1786             case BuiltinType::Long:
1787             case BuiltinType::LongLong:
1788             case BuiltinType::Int128:        return lldb::eEncodingSint;
1789 
1790             case BuiltinType::Char_U:
1791             case BuiltinType::UChar:
1792             case BuiltinType::WChar_U:
1793             case BuiltinType::UShort:
1794             case BuiltinType::UInt:
1795             case BuiltinType::ULong:
1796             case BuiltinType::ULongLong:
1797             case BuiltinType::UInt128:       return lldb::eEncodingUint;
1798 
1799             case BuiltinType::Float:
1800             case BuiltinType::Double:
1801             case BuiltinType::LongDouble:    return lldb::eEncodingIEEE754;
1802 
1803             case BuiltinType::ObjCClass:
1804             case BuiltinType::ObjCId:
1805             case BuiltinType::ObjCSel:       return lldb::eEncodingUint;
1806 
1807             case BuiltinType::NullPtr:       return lldb::eEncodingUint;
1808         }
1809             break;
1810             // All pointer types are represented as unsigned integer encodings.
1811             // We may nee to add a eEncodingPointer if we ever need to know the
1812             // difference
1813         case clang::Type::ObjCObjectPointer:
1814         case clang::Type::BlockPointer:
1815         case clang::Type::Pointer:
1816         case clang::Type::LValueReference:
1817         case clang::Type::RValueReference:
1818         case clang::Type::MemberPointer:            return lldb::eEncodingUint;
1819         case clang::Type::Complex:
1820         {
1821             lldb::Encoding encoding = lldb::eEncodingIEEE754;
1822             if (qual_type->isComplexType())
1823                 encoding = lldb::eEncodingIEEE754;
1824             else
1825             {
1826                 const ComplexType *complex_type = qual_type->getAsComplexIntegerType();
1827                 if (complex_type)
1828                     encoding = ClangASTType(m_ast, complex_type->getElementType()).GetEncoding(count);
1829                 else
1830                     encoding = lldb::eEncodingSint;
1831             }
1832             count = 2;
1833             return encoding;
1834         }
1835 
1836         case clang::Type::ObjCInterface:            break;
1837         case clang::Type::Record:                   break;
1838         case clang::Type::Enum:                     return lldb::eEncodingSint;
1839         case clang::Type::Typedef:
1840             return ClangASTType(m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetEncoding(count);
1841 
1842         case clang::Type::Elaborated:
1843             return ClangASTType(m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetEncoding(count);
1844 
1845         case clang::Type::Paren:
1846             return ClangASTType(m_ast, cast<ParenType>(qual_type)->desugar()).GetEncoding(count);
1847 
1848         case clang::Type::DependentSizedArray:
1849         case clang::Type::DependentSizedExtVector:
1850         case clang::Type::UnresolvedUsing:
1851         case clang::Type::Attributed:
1852         case clang::Type::TemplateTypeParm:
1853         case clang::Type::SubstTemplateTypeParm:
1854         case clang::Type::SubstTemplateTypeParmPack:
1855         case clang::Type::Auto:
1856         case clang::Type::InjectedClassName:
1857         case clang::Type::DependentName:
1858         case clang::Type::DependentTemplateSpecialization:
1859         case clang::Type::PackExpansion:
1860         case clang::Type::ObjCObject:
1861 
1862         case clang::Type::TypeOfExpr:
1863         case clang::Type::TypeOf:
1864         case clang::Type::Decltype:
1865         case clang::Type::TemplateSpecialization:
1866         case clang::Type::Atomic:
1867             break;
1868 
1869     }
1870     count = 0;
1871     return lldb::eEncodingInvalid;
1872 }
1873 
1874 lldb::Format
GetFormat() const1875 ClangASTType::GetFormat () const
1876 {
1877     if (!IsValid())
1878         return lldb::eFormatDefault;
1879 
1880     QualType qual_type(GetCanonicalQualType());
1881 
1882     switch (qual_type->getTypeClass())
1883     {
1884         case clang::Type::UnaryTransform:
1885             break;
1886 
1887         case clang::Type::FunctionNoProto:
1888         case clang::Type::FunctionProto:
1889             break;
1890 
1891         case clang::Type::IncompleteArray:
1892         case clang::Type::VariableArray:
1893             break;
1894 
1895         case clang::Type::ConstantArray:
1896             return lldb::eFormatVoid; // no value
1897 
1898         case clang::Type::ExtVector:
1899         case clang::Type::Vector:
1900             break;
1901 
1902         case clang::Type::Builtin:
1903             switch (cast<BuiltinType>(qual_type)->getKind())
1904         {
1905                 //default: assert(0 && "Unknown builtin type!");
1906             case BuiltinType::UnknownAny:
1907             case BuiltinType::Void:
1908             case BuiltinType::BoundMember:
1909                 break;
1910 
1911             case BuiltinType::Bool:          return lldb::eFormatBoolean;
1912             case BuiltinType::Char_S:
1913             case BuiltinType::SChar:
1914             case BuiltinType::WChar_S:
1915             case BuiltinType::Char_U:
1916             case BuiltinType::UChar:
1917             case BuiltinType::WChar_U:       return lldb::eFormatChar;
1918             case BuiltinType::Char16:        return lldb::eFormatUnicode16;
1919             case BuiltinType::Char32:        return lldb::eFormatUnicode32;
1920             case BuiltinType::UShort:        return lldb::eFormatUnsigned;
1921             case BuiltinType::Short:         return lldb::eFormatDecimal;
1922             case BuiltinType::UInt:          return lldb::eFormatUnsigned;
1923             case BuiltinType::Int:           return lldb::eFormatDecimal;
1924             case BuiltinType::ULong:         return lldb::eFormatUnsigned;
1925             case BuiltinType::Long:          return lldb::eFormatDecimal;
1926             case BuiltinType::ULongLong:     return lldb::eFormatUnsigned;
1927             case BuiltinType::LongLong:      return lldb::eFormatDecimal;
1928             case BuiltinType::UInt128:       return lldb::eFormatUnsigned;
1929             case BuiltinType::Int128:        return lldb::eFormatDecimal;
1930             case BuiltinType::Float:         return lldb::eFormatFloat;
1931             case BuiltinType::Double:        return lldb::eFormatFloat;
1932             case BuiltinType::LongDouble:    return lldb::eFormatFloat;
1933             case BuiltinType::NullPtr:
1934             case BuiltinType::Overload:
1935             case BuiltinType::Dependent:
1936             case BuiltinType::ObjCId:
1937             case BuiltinType::ObjCClass:
1938             case BuiltinType::ObjCSel:
1939             case BuiltinType::Half:
1940             case BuiltinType::ARCUnbridgedCast:
1941             case BuiltinType::PseudoObject:
1942             case BuiltinType::BuiltinFn:
1943             case BuiltinType::OCLEvent:
1944             case BuiltinType::OCLImage1d:
1945             case BuiltinType::OCLImage1dArray:
1946             case BuiltinType::OCLImage1dBuffer:
1947             case BuiltinType::OCLImage2d:
1948             case BuiltinType::OCLImage2dArray:
1949             case BuiltinType::OCLImage3d:
1950             case BuiltinType::OCLSampler:
1951                 return lldb::eFormatHex;
1952         }
1953             break;
1954         case clang::Type::ObjCObjectPointer:        return lldb::eFormatHex;
1955         case clang::Type::BlockPointer:             return lldb::eFormatHex;
1956         case clang::Type::Pointer:                  return lldb::eFormatHex;
1957         case clang::Type::LValueReference:
1958         case clang::Type::RValueReference:          return lldb::eFormatHex;
1959         case clang::Type::MemberPointer:            break;
1960         case clang::Type::Complex:
1961         {
1962             if (qual_type->isComplexType())
1963                 return lldb::eFormatComplex;
1964             else
1965                 return lldb::eFormatComplexInteger;
1966         }
1967         case clang::Type::ObjCInterface:            break;
1968         case clang::Type::Record:                   break;
1969         case clang::Type::Enum:                     return lldb::eFormatEnum;
1970         case clang::Type::Typedef:
1971             return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetFormat();
1972         case clang::Type::Auto:
1973             return ClangASTType (m_ast, cast<AutoType>(qual_type)->desugar()).GetFormat();
1974         case clang::Type::Paren:
1975             return ClangASTType (m_ast, cast<ParenType>(qual_type)->desugar()).GetFormat();
1976         case clang::Type::Elaborated:
1977             return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetFormat();
1978         case clang::Type::DependentSizedArray:
1979         case clang::Type::DependentSizedExtVector:
1980         case clang::Type::UnresolvedUsing:
1981         case clang::Type::Attributed:
1982         case clang::Type::TemplateTypeParm:
1983         case clang::Type::SubstTemplateTypeParm:
1984         case clang::Type::SubstTemplateTypeParmPack:
1985         case clang::Type::InjectedClassName:
1986         case clang::Type::DependentName:
1987         case clang::Type::DependentTemplateSpecialization:
1988         case clang::Type::PackExpansion:
1989         case clang::Type::ObjCObject:
1990 
1991         case clang::Type::TypeOfExpr:
1992         case clang::Type::TypeOf:
1993         case clang::Type::Decltype:
1994         case clang::Type::TemplateSpecialization:
1995         case clang::Type::Atomic:
1996             break;
1997     }
1998     // We don't know hot to display this type...
1999     return lldb::eFormatBytes;
2000 }
2001 
2002 static bool
ObjCDeclHasIVars(ObjCInterfaceDecl * class_interface_decl,bool check_superclass)2003 ObjCDeclHasIVars (ObjCInterfaceDecl *class_interface_decl, bool check_superclass)
2004 {
2005     while (class_interface_decl)
2006     {
2007         if (class_interface_decl->ivar_size() > 0)
2008             return true;
2009 
2010         if (check_superclass)
2011             class_interface_decl = class_interface_decl->getSuperClass();
2012         else
2013             break;
2014     }
2015     return false;
2016 }
2017 
2018 uint32_t
GetNumChildren(bool omit_empty_base_classes) const2019 ClangASTType::GetNumChildren (bool omit_empty_base_classes) const
2020 {
2021     if (!IsValid())
2022         return 0;
2023 
2024     uint32_t num_children = 0;
2025     QualType qual_type(GetQualType());
2026     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2027     switch (type_class)
2028     {
2029         case clang::Type::Builtin:
2030             switch (cast<BuiltinType>(qual_type)->getKind())
2031         {
2032             case BuiltinType::ObjCId:    // child is Class
2033             case BuiltinType::ObjCClass: // child is Class
2034                 num_children = 1;
2035                 break;
2036 
2037             default:
2038                 break;
2039         }
2040             break;
2041 
2042         case clang::Type::Complex: return 0;
2043 
2044         case clang::Type::Record:
2045             if (GetCompleteQualType (m_ast, qual_type))
2046             {
2047                 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
2048                 const RecordDecl *record_decl = record_type->getDecl();
2049                 assert(record_decl);
2050                 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2051                 if (cxx_record_decl)
2052                 {
2053                     if (omit_empty_base_classes)
2054                     {
2055                         // Check each base classes to see if it or any of its
2056                         // base classes contain any fields. This can help
2057                         // limit the noise in variable views by not having to
2058                         // show base classes that contain no members.
2059                         CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2060                         for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2061                              base_class != base_class_end;
2062                              ++base_class)
2063                         {
2064                             const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2065 
2066                             // Skip empty base classes
2067                             if (ClangASTContext::RecordHasFields(base_class_decl) == false)
2068                                 continue;
2069 
2070                             num_children++;
2071                         }
2072                     }
2073                     else
2074                     {
2075                         // Include all base classes
2076                         num_children += cxx_record_decl->getNumBases();
2077                     }
2078 
2079                 }
2080                 RecordDecl::field_iterator field, field_end;
2081                 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
2082                     ++num_children;
2083             }
2084             break;
2085 
2086         case clang::Type::ObjCObject:
2087         case clang::Type::ObjCInterface:
2088             if (GetCompleteQualType (m_ast, qual_type))
2089             {
2090                 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2091                 assert (objc_class_type);
2092                 if (objc_class_type)
2093                 {
2094                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2095 
2096                     if (class_interface_decl)
2097                     {
2098 
2099                         ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2100                         if (superclass_interface_decl)
2101                         {
2102                             if (omit_empty_base_classes)
2103                             {
2104                                 if (ObjCDeclHasIVars (superclass_interface_decl, true))
2105                                     ++num_children;
2106                             }
2107                             else
2108                                 ++num_children;
2109                         }
2110 
2111                         num_children += class_interface_decl->ivar_size();
2112                     }
2113                 }
2114             }
2115             break;
2116 
2117         case clang::Type::ObjCObjectPointer:
2118         {
2119             const ObjCObjectPointerType *pointer_type = cast<ObjCObjectPointerType>(qual_type.getTypePtr());
2120             QualType pointee_type = pointer_type->getPointeeType();
2121             uint32_t num_pointee_children = ClangASTType (m_ast,pointee_type).GetNumChildren (omit_empty_base_classes);
2122             // If this type points to a simple type, then it has 1 child
2123             if (num_pointee_children == 0)
2124                 num_children = 1;
2125             else
2126                 num_children = num_pointee_children;
2127         }
2128             break;
2129 
2130         case clang::Type::Vector:
2131         case clang::Type::ExtVector:
2132             num_children = cast<VectorType>(qual_type.getTypePtr())->getNumElements();
2133             break;
2134 
2135         case clang::Type::ConstantArray:
2136             num_children = cast<ConstantArrayType>(qual_type.getTypePtr())->getSize().getLimitedValue();
2137             break;
2138 
2139         case clang::Type::Pointer:
2140         {
2141             const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
2142             QualType pointee_type (pointer_type->getPointeeType());
2143             uint32_t num_pointee_children = ClangASTType (m_ast,pointee_type).GetNumChildren (omit_empty_base_classes);
2144             if (num_pointee_children == 0)
2145             {
2146                 // We have a pointer to a pointee type that claims it has no children.
2147                 // We will want to look at
2148                 num_children = ClangASTType (m_ast, pointee_type).GetNumPointeeChildren();
2149             }
2150             else
2151                 num_children = num_pointee_children;
2152         }
2153             break;
2154 
2155         case clang::Type::LValueReference:
2156         case clang::Type::RValueReference:
2157         {
2158             const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
2159             QualType pointee_type = reference_type->getPointeeType();
2160             uint32_t num_pointee_children = ClangASTType (m_ast, pointee_type).GetNumChildren (omit_empty_base_classes);
2161             // If this type points to a simple type, then it has 1 child
2162             if (num_pointee_children == 0)
2163                 num_children = 1;
2164             else
2165                 num_children = num_pointee_children;
2166         }
2167             break;
2168 
2169 
2170         case clang::Type::Typedef:
2171             num_children = ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumChildren (omit_empty_base_classes);
2172             break;
2173 
2174         case clang::Type::Elaborated:
2175             num_children = ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetNumChildren (omit_empty_base_classes);
2176             break;
2177 
2178         case clang::Type::Paren:
2179             num_children = ClangASTType (m_ast, cast<ParenType>(qual_type)->desugar()).GetNumChildren (omit_empty_base_classes);
2180             break;
2181         default:
2182             break;
2183     }
2184     return num_children;
2185 }
2186 
2187 lldb::BasicType
GetBasicTypeEnumeration() const2188 ClangASTType::GetBasicTypeEnumeration () const
2189 {
2190     if (IsValid())
2191     {
2192         QualType qual_type(GetQualType());
2193         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2194         if (type_class == clang::Type::Builtin)
2195         {
2196             switch (cast<clang::BuiltinType>(qual_type)->getKind())
2197             {
2198                 case clang::BuiltinType::Void:      return eBasicTypeVoid;
2199                 case clang::BuiltinType::Bool:      return eBasicTypeBool;
2200                 case clang::BuiltinType::Char_S:    return eBasicTypeSignedChar;
2201                 case clang::BuiltinType::Char_U:    return eBasicTypeUnsignedChar;
2202                 case clang::BuiltinType::Char16:    return eBasicTypeChar16;
2203                 case clang::BuiltinType::Char32:    return eBasicTypeChar32;
2204                 case clang::BuiltinType::UChar:     return eBasicTypeUnsignedChar;
2205                 case clang::BuiltinType::SChar:     return eBasicTypeSignedChar;
2206                 case clang::BuiltinType::WChar_S:   return eBasicTypeSignedWChar;
2207                 case clang::BuiltinType::WChar_U:   return eBasicTypeUnsignedWChar;
2208                 case clang::BuiltinType::Short:     return eBasicTypeShort;
2209                 case clang::BuiltinType::UShort:    return eBasicTypeUnsignedShort;
2210                 case clang::BuiltinType::Int:       return eBasicTypeInt;
2211                 case clang::BuiltinType::UInt:      return eBasicTypeUnsignedInt;
2212                 case clang::BuiltinType::Long:      return eBasicTypeLong;
2213                 case clang::BuiltinType::ULong:     return eBasicTypeUnsignedLong;
2214                 case clang::BuiltinType::LongLong:  return eBasicTypeLongLong;
2215                 case clang::BuiltinType::ULongLong: return eBasicTypeUnsignedLongLong;
2216                 case clang::BuiltinType::Int128:    return eBasicTypeInt128;
2217                 case clang::BuiltinType::UInt128:   return eBasicTypeUnsignedInt128;
2218 
2219                 case clang::BuiltinType::Half:      return eBasicTypeHalf;
2220                 case clang::BuiltinType::Float:     return eBasicTypeFloat;
2221                 case clang::BuiltinType::Double:    return eBasicTypeDouble;
2222                 case clang::BuiltinType::LongDouble:return eBasicTypeLongDouble;
2223 
2224                 case clang::BuiltinType::NullPtr:   return eBasicTypeNullPtr;
2225                 case clang::BuiltinType::ObjCId:    return eBasicTypeObjCID;
2226                 case clang::BuiltinType::ObjCClass: return eBasicTypeObjCClass;
2227                 case clang::BuiltinType::ObjCSel:   return eBasicTypeObjCSel;
2228                 case clang::BuiltinType::Dependent:
2229                 case clang::BuiltinType::Overload:
2230                 case clang::BuiltinType::BoundMember:
2231                 case clang::BuiltinType::PseudoObject:
2232                 case clang::BuiltinType::UnknownAny:
2233                 case clang::BuiltinType::BuiltinFn:
2234                 case clang::BuiltinType::ARCUnbridgedCast:
2235                 case clang::BuiltinType::OCLEvent:
2236                 case clang::BuiltinType::OCLImage1d:
2237                 case clang::BuiltinType::OCLImage1dArray:
2238                 case clang::BuiltinType::OCLImage1dBuffer:
2239                 case clang::BuiltinType::OCLImage2d:
2240                 case clang::BuiltinType::OCLImage2dArray:
2241                 case clang::BuiltinType::OCLImage3d:
2242                 case clang::BuiltinType::OCLSampler:
2243                     return eBasicTypeOther;
2244             }
2245         }
2246     }
2247     return eBasicTypeInvalid;
2248 }
2249 
2250 
2251 #pragma mark Aggregate Types
2252 
2253 uint32_t
GetNumDirectBaseClasses() const2254 ClangASTType::GetNumDirectBaseClasses () const
2255 {
2256     if (!IsValid())
2257         return 0;
2258 
2259     uint32_t count = 0;
2260     QualType qual_type(GetCanonicalQualType());
2261     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2262     switch (type_class)
2263     {
2264         case clang::Type::Record:
2265             if (GetCompleteType())
2266             {
2267                 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2268                 if (cxx_record_decl)
2269                     count = cxx_record_decl->getNumBases();
2270             }
2271             break;
2272 
2273         case clang::Type::ObjCObjectPointer:
2274             if (GetCompleteType())
2275             {
2276                 const ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
2277                 if (objc_class_type)
2278                 {
2279                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
2280                     if (class_interface_decl && class_interface_decl->getSuperClass())
2281                         count = 1;
2282                 }
2283             }
2284             break;
2285 
2286         case clang::Type::ObjCObject:
2287         case clang::Type::ObjCInterface:
2288             if (GetCompleteType())
2289             {
2290                 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
2291                 if (objc_class_type)
2292                 {
2293                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2294 
2295                     if (class_interface_decl && class_interface_decl->getSuperClass())
2296                         count = 1;
2297                 }
2298             }
2299             break;
2300 
2301 
2302         case clang::Type::Typedef:
2303             count = ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumDirectBaseClasses ();
2304             break;
2305 
2306         case clang::Type::Elaborated:
2307             count = ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetNumDirectBaseClasses ();
2308             break;
2309 
2310         case clang::Type::Paren:
2311             return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetNumDirectBaseClasses ();
2312 
2313         default:
2314             break;
2315     }
2316     return count;
2317 }
2318 
2319 uint32_t
GetNumVirtualBaseClasses() const2320 ClangASTType::GetNumVirtualBaseClasses () const
2321 {
2322     if (!IsValid())
2323         return 0;
2324 
2325     uint32_t count = 0;
2326     QualType qual_type(GetCanonicalQualType());
2327     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2328     switch (type_class)
2329     {
2330         case clang::Type::Record:
2331             if (GetCompleteType())
2332             {
2333                 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2334                 if (cxx_record_decl)
2335                     count = cxx_record_decl->getNumVBases();
2336             }
2337             break;
2338 
2339         case clang::Type::Typedef:
2340             count = ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumVirtualBaseClasses();
2341             break;
2342 
2343         case clang::Type::Elaborated:
2344             count = ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetNumVirtualBaseClasses();
2345             break;
2346 
2347         case clang::Type::Paren:
2348             count = ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetNumVirtualBaseClasses();
2349             break;
2350 
2351         default:
2352             break;
2353     }
2354     return count;
2355 }
2356 
2357 uint32_t
GetNumFields() const2358 ClangASTType::GetNumFields () const
2359 {
2360     if (!IsValid())
2361         return 0;
2362 
2363     uint32_t count = 0;
2364     QualType qual_type(GetCanonicalQualType());
2365     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2366     switch (type_class)
2367     {
2368         case clang::Type::Record:
2369             if (GetCompleteType())
2370             {
2371                 const RecordType *record_type = dyn_cast<RecordType>(qual_type.getTypePtr());
2372                 if (record_type)
2373                 {
2374                     RecordDecl *record_decl = record_type->getDecl();
2375                     if (record_decl)
2376                     {
2377                         uint32_t field_idx = 0;
2378                         RecordDecl::field_iterator field, field_end;
2379                         for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field)
2380                             ++field_idx;
2381                         count = field_idx;
2382                     }
2383                 }
2384             }
2385             break;
2386 
2387         case clang::Type::Typedef:
2388             count = ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumFields();
2389             break;
2390 
2391         case clang::Type::Elaborated:
2392             count = ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetNumFields();
2393             break;
2394 
2395         case clang::Type::Paren:
2396             count = ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetNumFields();
2397             break;
2398 
2399         case clang::Type::ObjCObjectPointer:
2400             if (GetCompleteType())
2401             {
2402                 const ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
2403                 if (objc_class_type)
2404                 {
2405                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
2406 
2407                     if (class_interface_decl)
2408                         count = class_interface_decl->ivar_size();
2409                 }
2410             }
2411             break;
2412 
2413         case clang::Type::ObjCObject:
2414         case clang::Type::ObjCInterface:
2415             if (GetCompleteType())
2416             {
2417                 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2418                 if (objc_class_type)
2419                 {
2420                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2421 
2422                     if (class_interface_decl)
2423                         count = class_interface_decl->ivar_size();
2424                 }
2425             }
2426             break;
2427 
2428         default:
2429             break;
2430     }
2431     return count;
2432 }
2433 
2434 ClangASTType
GetDirectBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const2435 ClangASTType::GetDirectBaseClassAtIndex (size_t idx, uint32_t *bit_offset_ptr) const
2436 {
2437     if (!IsValid())
2438         return ClangASTType();
2439 
2440     QualType qual_type(GetCanonicalQualType());
2441     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2442     switch (type_class)
2443     {
2444         case clang::Type::Record:
2445             if (GetCompleteType())
2446             {
2447                 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2448                 if (cxx_record_decl)
2449                 {
2450                     uint32_t curr_idx = 0;
2451                     CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2452                     for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2453                          base_class != base_class_end;
2454                          ++base_class, ++curr_idx)
2455                     {
2456                         if (curr_idx == idx)
2457                         {
2458                             if (bit_offset_ptr)
2459                             {
2460                                 const ASTRecordLayout &record_layout = m_ast->getASTRecordLayout(cxx_record_decl);
2461                                 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2462                                 if (base_class->isVirtual())
2463                                     *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
2464                                 else
2465                                     *bit_offset_ptr = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
2466                             }
2467                             return ClangASTType (m_ast, base_class->getType());
2468                         }
2469                     }
2470                 }
2471             }
2472             break;
2473 
2474         case clang::Type::ObjCObjectPointer:
2475             if (idx == 0 && GetCompleteType())
2476             {
2477                 const ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
2478                 if (objc_class_type)
2479                 {
2480                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
2481                     if (class_interface_decl)
2482                     {
2483                         ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2484                         if (superclass_interface_decl)
2485                         {
2486                             if (bit_offset_ptr)
2487                                 *bit_offset_ptr = 0;
2488                             return ClangASTType (m_ast, m_ast->getObjCInterfaceType(superclass_interface_decl));
2489                         }
2490                     }
2491                 }
2492             }
2493             break;
2494 
2495         case clang::Type::ObjCObject:
2496         case clang::Type::ObjCInterface:
2497             if (idx == 0 && GetCompleteType())
2498             {
2499                 const ObjCObjectType *objc_class_type = qual_type->getAsObjCQualifiedInterfaceType();
2500                 if (objc_class_type)
2501                 {
2502                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2503 
2504                     if (class_interface_decl)
2505                     {
2506                         ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
2507                         if (superclass_interface_decl)
2508                         {
2509                             if (bit_offset_ptr)
2510                                 *bit_offset_ptr = 0;
2511                             return ClangASTType (m_ast, m_ast->getObjCInterfaceType(superclass_interface_decl));
2512                         }
2513                     }
2514                 }
2515             }
2516             break;
2517 
2518 
2519         case clang::Type::Typedef:
2520             return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetDirectBaseClassAtIndex (idx, bit_offset_ptr);
2521 
2522         case clang::Type::Elaborated:
2523             return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetDirectBaseClassAtIndex (idx, bit_offset_ptr);
2524 
2525         case clang::Type::Paren:
2526             return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetDirectBaseClassAtIndex (idx, bit_offset_ptr);
2527 
2528         default:
2529             break;
2530     }
2531     return ClangASTType();
2532 }
2533 
2534 ClangASTType
GetVirtualBaseClassAtIndex(size_t idx,uint32_t * bit_offset_ptr) const2535 ClangASTType::GetVirtualBaseClassAtIndex (size_t idx, uint32_t *bit_offset_ptr) const
2536 {
2537     if (!IsValid())
2538         return ClangASTType();
2539 
2540     QualType qual_type(GetCanonicalQualType());
2541     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2542     switch (type_class)
2543     {
2544         case clang::Type::Record:
2545             if (GetCompleteType())
2546             {
2547                 const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
2548                 if (cxx_record_decl)
2549                 {
2550                     uint32_t curr_idx = 0;
2551                     CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2552                     for (base_class = cxx_record_decl->vbases_begin(), base_class_end = cxx_record_decl->vbases_end();
2553                          base_class != base_class_end;
2554                          ++base_class, ++curr_idx)
2555                     {
2556                         if (curr_idx == idx)
2557                         {
2558                             if (bit_offset_ptr)
2559                             {
2560                                 const ASTRecordLayout &record_layout = m_ast->getASTRecordLayout(cxx_record_decl);
2561                                 const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2562                                 *bit_offset_ptr = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
2563 
2564                             }
2565                             return ClangASTType (m_ast, base_class->getType());
2566                         }
2567                     }
2568                 }
2569             }
2570             break;
2571 
2572         case clang::Type::Typedef:
2573             return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetVirtualBaseClassAtIndex (idx, bit_offset_ptr);
2574 
2575         case clang::Type::Elaborated:
2576             return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetVirtualBaseClassAtIndex (idx, bit_offset_ptr);
2577 
2578         case clang::Type::Paren:
2579             return  ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetVirtualBaseClassAtIndex (idx, bit_offset_ptr);
2580 
2581         default:
2582             break;
2583     }
2584     return ClangASTType();
2585 }
2586 
2587 static clang_type_t
GetObjCFieldAtIndex(clang::ASTContext * ast,ObjCInterfaceDecl * class_interface_decl,size_t idx,std::string & name,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr)2588 GetObjCFieldAtIndex (clang::ASTContext *ast,
2589                      ObjCInterfaceDecl *class_interface_decl,
2590                      size_t idx,
2591                      std::string& name,
2592                      uint64_t *bit_offset_ptr,
2593                      uint32_t *bitfield_bit_size_ptr,
2594                      bool *is_bitfield_ptr)
2595 {
2596     if (class_interface_decl)
2597     {
2598         if (idx < (class_interface_decl->ivar_size()))
2599         {
2600             ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
2601             uint32_t ivar_idx = 0;
2602 
2603             for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++ivar_idx)
2604             {
2605                 if (ivar_idx == idx)
2606                 {
2607                     const ObjCIvarDecl* ivar_decl = *ivar_pos;
2608 
2609                     QualType ivar_qual_type(ivar_decl->getType());
2610 
2611                     name.assign(ivar_decl->getNameAsString());
2612 
2613                     if (bit_offset_ptr)
2614                     {
2615                         const ASTRecordLayout &interface_layout = ast->getASTObjCInterfaceLayout(class_interface_decl);
2616                         *bit_offset_ptr = interface_layout.getFieldOffset (ivar_idx);
2617                     }
2618 
2619                     const bool is_bitfield = ivar_pos->isBitField();
2620 
2621                     if (bitfield_bit_size_ptr)
2622                     {
2623                         *bitfield_bit_size_ptr = 0;
2624 
2625                         if (is_bitfield && ast)
2626                         {
2627                             Expr *bitfield_bit_size_expr = ivar_pos->getBitWidth();
2628                             llvm::APSInt bitfield_apsint;
2629                             if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *ast))
2630                             {
2631                                 *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
2632                             }
2633                         }
2634                     }
2635                     if (is_bitfield_ptr)
2636                         *is_bitfield_ptr = is_bitfield;
2637 
2638                     return ivar_qual_type.getAsOpaquePtr();
2639                 }
2640             }
2641         }
2642     }
2643     return NULL;
2644 }
2645 
2646 ClangASTType
GetFieldAtIndex(size_t idx,std::string & name,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const2647 ClangASTType::GetFieldAtIndex (size_t idx,
2648                                std::string& name,
2649                                uint64_t *bit_offset_ptr,
2650                                uint32_t *bitfield_bit_size_ptr,
2651                                bool *is_bitfield_ptr) const
2652 {
2653     if (!IsValid())
2654         return ClangASTType();
2655 
2656     QualType qual_type(GetCanonicalQualType());
2657     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2658     switch (type_class)
2659     {
2660         case clang::Type::Record:
2661             if (GetCompleteType())
2662             {
2663                 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
2664                 const RecordDecl *record_decl = record_type->getDecl();
2665                 uint32_t field_idx = 0;
2666                 RecordDecl::field_iterator field, field_end;
2667                 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx)
2668                 {
2669                     if (idx == field_idx)
2670                     {
2671                         // Print the member type if requested
2672                         // Print the member name and equal sign
2673                         name.assign(field->getNameAsString());
2674 
2675                         // Figure out the type byte size (field_type_info.first) and
2676                         // alignment (field_type_info.second) from the AST context.
2677                         if (bit_offset_ptr)
2678                         {
2679                             const ASTRecordLayout &record_layout = m_ast->getASTRecordLayout(record_decl);
2680                             *bit_offset_ptr = record_layout.getFieldOffset (field_idx);
2681                         }
2682 
2683                         const bool is_bitfield = field->isBitField();
2684 
2685                         if (bitfield_bit_size_ptr)
2686                         {
2687                             *bitfield_bit_size_ptr = 0;
2688 
2689                             if (is_bitfield)
2690                             {
2691                                 Expr *bitfield_bit_size_expr = field->getBitWidth();
2692                                 llvm::APSInt bitfield_apsint;
2693                                 if (bitfield_bit_size_expr && bitfield_bit_size_expr->EvaluateAsInt(bitfield_apsint, *m_ast))
2694                                 {
2695                                     *bitfield_bit_size_ptr = bitfield_apsint.getLimitedValue();
2696                                 }
2697                             }
2698                         }
2699                         if (is_bitfield_ptr)
2700                             *is_bitfield_ptr = is_bitfield;
2701 
2702                         return ClangASTType (m_ast, field->getType());
2703                     }
2704                 }
2705             }
2706             break;
2707 
2708         case clang::Type::ObjCObjectPointer:
2709             if (GetCompleteType())
2710             {
2711                 const ObjCObjectPointerType *objc_class_type = qual_type->getAsObjCInterfacePointerType();
2712                 if (objc_class_type)
2713                 {
2714                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterfaceDecl();
2715                     return ClangASTType (m_ast, GetObjCFieldAtIndex(m_ast, class_interface_decl, idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr));
2716                 }
2717             }
2718             break;
2719 
2720         case clang::Type::ObjCObject:
2721         case clang::Type::ObjCInterface:
2722             if (GetCompleteType())
2723             {
2724                 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
2725                 assert (objc_class_type);
2726                 if (objc_class_type)
2727                 {
2728                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
2729                     return ClangASTType (m_ast, GetObjCFieldAtIndex(m_ast, class_interface_decl, idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr));
2730                 }
2731             }
2732             break;
2733 
2734 
2735         case clang::Type::Typedef:
2736             return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).
2737                         GetFieldAtIndex (idx,
2738                                          name,
2739                                          bit_offset_ptr,
2740                                          bitfield_bit_size_ptr,
2741                                          is_bitfield_ptr);
2742 
2743         case clang::Type::Elaborated:
2744             return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).
2745                         GetFieldAtIndex (idx,
2746                                          name,
2747                                          bit_offset_ptr,
2748                                          bitfield_bit_size_ptr,
2749                                          is_bitfield_ptr);
2750 
2751         case clang::Type::Paren:
2752             return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).
2753                         GetFieldAtIndex (idx,
2754                                          name,
2755                                          bit_offset_ptr,
2756                                          bitfield_bit_size_ptr,
2757                                          is_bitfield_ptr);
2758 
2759         default:
2760             break;
2761     }
2762     return ClangASTType();
2763 }
2764 
2765 uint32_t
GetIndexOfFieldWithName(const char * name,ClangASTType * field_clang_type_ptr,uint64_t * bit_offset_ptr,uint32_t * bitfield_bit_size_ptr,bool * is_bitfield_ptr) const2766 ClangASTType::GetIndexOfFieldWithName (const char* name,
2767                                        ClangASTType* field_clang_type_ptr,
2768                                        uint64_t *bit_offset_ptr,
2769                                        uint32_t *bitfield_bit_size_ptr,
2770                                        bool *is_bitfield_ptr) const
2771 {
2772     unsigned count = GetNumFields();
2773     std::string field_name;
2774     for (unsigned index = 0; index < count; index++)
2775     {
2776         ClangASTType field_clang_type (GetFieldAtIndex(index, field_name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr));
2777         if (strcmp(field_name.c_str(), name) == 0)
2778         {
2779             if (field_clang_type_ptr)
2780                 *field_clang_type_ptr = field_clang_type;
2781             return index;
2782         }
2783     }
2784     return UINT32_MAX;
2785 }
2786 
2787 // If a pointer to a pointee type (the clang_type arg) says that it has no
2788 // children, then we either need to trust it, or override it and return a
2789 // different result. For example, an "int *" has one child that is an integer,
2790 // but a function pointer doesn't have any children. Likewise if a Record type
2791 // claims it has no children, then there really is nothing to show.
2792 uint32_t
GetNumPointeeChildren() const2793 ClangASTType::GetNumPointeeChildren () const
2794 {
2795     if (!IsValid())
2796         return 0;
2797 
2798     QualType qual_type(GetCanonicalQualType());
2799     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
2800     switch (type_class)
2801     {
2802         case clang::Type::Builtin:
2803             switch (cast<clang::BuiltinType>(qual_type)->getKind())
2804         {
2805             case clang::BuiltinType::UnknownAny:
2806             case clang::BuiltinType::Void:
2807             case clang::BuiltinType::NullPtr:
2808             case clang::BuiltinType::OCLEvent:
2809             case clang::BuiltinType::OCLImage1d:
2810             case clang::BuiltinType::OCLImage1dArray:
2811             case clang::BuiltinType::OCLImage1dBuffer:
2812             case clang::BuiltinType::OCLImage2d:
2813             case clang::BuiltinType::OCLImage2dArray:
2814             case clang::BuiltinType::OCLImage3d:
2815             case clang::BuiltinType::OCLSampler:
2816                 return 0;
2817             case clang::BuiltinType::Bool:
2818             case clang::BuiltinType::Char_U:
2819             case clang::BuiltinType::UChar:
2820             case clang::BuiltinType::WChar_U:
2821             case clang::BuiltinType::Char16:
2822             case clang::BuiltinType::Char32:
2823             case clang::BuiltinType::UShort:
2824             case clang::BuiltinType::UInt:
2825             case clang::BuiltinType::ULong:
2826             case clang::BuiltinType::ULongLong:
2827             case clang::BuiltinType::UInt128:
2828             case clang::BuiltinType::Char_S:
2829             case clang::BuiltinType::SChar:
2830             case clang::BuiltinType::WChar_S:
2831             case clang::BuiltinType::Short:
2832             case clang::BuiltinType::Int:
2833             case clang::BuiltinType::Long:
2834             case clang::BuiltinType::LongLong:
2835             case clang::BuiltinType::Int128:
2836             case clang::BuiltinType::Float:
2837             case clang::BuiltinType::Double:
2838             case clang::BuiltinType::LongDouble:
2839             case clang::BuiltinType::Dependent:
2840             case clang::BuiltinType::Overload:
2841             case clang::BuiltinType::ObjCId:
2842             case clang::BuiltinType::ObjCClass:
2843             case clang::BuiltinType::ObjCSel:
2844             case clang::BuiltinType::BoundMember:
2845             case clang::BuiltinType::Half:
2846             case clang::BuiltinType::ARCUnbridgedCast:
2847             case clang::BuiltinType::PseudoObject:
2848             case clang::BuiltinType::BuiltinFn:
2849                 return 1;
2850         }
2851             break;
2852 
2853         case clang::Type::Complex:                  return 1;
2854         case clang::Type::Pointer:                  return 1;
2855         case clang::Type::BlockPointer:             return 0;   // If block pointers don't have debug info, then no children for them
2856         case clang::Type::LValueReference:          return 1;
2857         case clang::Type::RValueReference:          return 1;
2858         case clang::Type::MemberPointer:            return 0;
2859         case clang::Type::ConstantArray:            return 0;
2860         case clang::Type::IncompleteArray:          return 0;
2861         case clang::Type::VariableArray:            return 0;
2862         case clang::Type::DependentSizedArray:      return 0;
2863         case clang::Type::DependentSizedExtVector:  return 0;
2864         case clang::Type::Vector:                   return 0;
2865         case clang::Type::ExtVector:                return 0;
2866         case clang::Type::FunctionProto:            return 0;   // When we function pointers, they have no children...
2867         case clang::Type::FunctionNoProto:          return 0;   // When we function pointers, they have no children...
2868         case clang::Type::UnresolvedUsing:          return 0;
2869         case clang::Type::Paren:                    return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetNumPointeeChildren ();
2870         case clang::Type::Typedef:                  return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumPointeeChildren ();
2871         case clang::Type::Elaborated:               return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetNumPointeeChildren ();
2872         case clang::Type::TypeOfExpr:               return 0;
2873         case clang::Type::TypeOf:                   return 0;
2874         case clang::Type::Decltype:                 return 0;
2875         case clang::Type::Record:                   return 0;
2876         case clang::Type::Enum:                     return 1;
2877         case clang::Type::TemplateTypeParm:         return 1;
2878         case clang::Type::SubstTemplateTypeParm:    return 1;
2879         case clang::Type::TemplateSpecialization:   return 1;
2880         case clang::Type::InjectedClassName:        return 0;
2881         case clang::Type::DependentName:            return 1;
2882         case clang::Type::DependentTemplateSpecialization:  return 1;
2883         case clang::Type::ObjCObject:               return 0;
2884         case clang::Type::ObjCInterface:            return 0;
2885         case clang::Type::ObjCObjectPointer:        return 1;
2886         default:
2887             break;
2888     }
2889     return 0;
2890 }
2891 
2892 
2893 ClangASTType
GetChildClangTypeAtIndex(ExecutionContext * exe_ctx,const char * parent_name,size_t idx,bool transparent_pointers,bool omit_empty_base_classes,bool ignore_array_bounds,std::string & child_name,uint32_t & child_byte_size,int32_t & child_byte_offset,uint32_t & child_bitfield_bit_size,uint32_t & child_bitfield_bit_offset,bool & child_is_base_class,bool & child_is_deref_of_parent) const2894 ClangASTType::GetChildClangTypeAtIndex (ExecutionContext *exe_ctx,
2895                                         const char *parent_name,
2896                                         size_t idx,
2897                                         bool transparent_pointers,
2898                                         bool omit_empty_base_classes,
2899                                         bool ignore_array_bounds,
2900                                         std::string& child_name,
2901                                         uint32_t &child_byte_size,
2902                                         int32_t &child_byte_offset,
2903                                         uint32_t &child_bitfield_bit_size,
2904                                         uint32_t &child_bitfield_bit_offset,
2905                                         bool &child_is_base_class,
2906                                         bool &child_is_deref_of_parent) const
2907 {
2908     if (!IsValid())
2909         return ClangASTType();
2910 
2911     QualType parent_qual_type(GetCanonicalQualType());
2912     const clang::Type::TypeClass parent_type_class = parent_qual_type->getTypeClass();
2913     child_bitfield_bit_size = 0;
2914     child_bitfield_bit_offset = 0;
2915     child_is_base_class = false;
2916 
2917     const bool idx_is_valid = idx < GetNumChildren (omit_empty_base_classes);
2918     uint32_t bit_offset;
2919     switch (parent_type_class)
2920     {
2921         case clang::Type::Builtin:
2922             if (idx_is_valid)
2923             {
2924                 switch (cast<clang::BuiltinType>(parent_qual_type)->getKind())
2925                 {
2926                     case clang::BuiltinType::ObjCId:
2927                     case clang::BuiltinType::ObjCClass:
2928                         child_name = "isa";
2929                         child_byte_size = m_ast->getTypeSize(m_ast->ObjCBuiltinClassTy) / CHAR_BIT;
2930                         return ClangASTType (m_ast, m_ast->ObjCBuiltinClassTy);
2931 
2932                     default:
2933                         break;
2934                 }
2935             }
2936             break;
2937 
2938         case clang::Type::Record:
2939             if (idx_is_valid && GetCompleteType())
2940             {
2941                 const RecordType *record_type = cast<RecordType>(parent_qual_type.getTypePtr());
2942                 const RecordDecl *record_decl = record_type->getDecl();
2943                 assert(record_decl);
2944                 const ASTRecordLayout &record_layout = m_ast->getASTRecordLayout(record_decl);
2945                 uint32_t child_idx = 0;
2946 
2947                 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
2948                 if (cxx_record_decl)
2949                 {
2950                     // We might have base classes to print out first
2951                     CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
2952                     for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
2953                          base_class != base_class_end;
2954                          ++base_class)
2955                     {
2956                         const CXXRecordDecl *base_class_decl = NULL;
2957 
2958                         // Skip empty base classes
2959                         if (omit_empty_base_classes)
2960                         {
2961                             base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2962                             if (ClangASTContext::RecordHasFields(base_class_decl) == false)
2963                                 continue;
2964                         }
2965 
2966                         if (idx == child_idx)
2967                         {
2968                             if (base_class_decl == NULL)
2969                                 base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
2970 
2971 
2972                             if (base_class->isVirtual())
2973                                 bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
2974                             else
2975                                 bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
2976 
2977                             // Base classes should be a multiple of 8 bits in size
2978                             child_byte_offset = bit_offset/8;
2979                             ClangASTType base_class_clang_type(m_ast, base_class->getType());
2980                             child_name = base_class_clang_type.GetTypeName();
2981                             uint64_t base_class_clang_type_bit_size = base_class_clang_type.GetBitSize();
2982 
2983                             // Base classes bit sizes should be a multiple of 8 bits in size
2984                             assert (base_class_clang_type_bit_size % 8 == 0);
2985                             child_byte_size = base_class_clang_type_bit_size / 8;
2986                             child_is_base_class = true;
2987                             return base_class_clang_type;
2988                         }
2989                         // We don't increment the child index in the for loop since we might
2990                         // be skipping empty base classes
2991                         ++child_idx;
2992                     }
2993                 }
2994                 // Make sure index is in range...
2995                 uint32_t field_idx = 0;
2996                 RecordDecl::field_iterator field, field_end;
2997                 for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
2998                 {
2999                     if (idx == child_idx)
3000                     {
3001                         // Print the member type if requested
3002                         // Print the member name and equal sign
3003                         child_name.assign(field->getNameAsString().c_str());
3004 
3005                         // Figure out the type byte size (field_type_info.first) and
3006                         // alignment (field_type_info.second) from the AST context.
3007                         ClangASTType field_clang_type (m_ast, field->getType());
3008                         assert(field_idx < record_layout.getFieldCount());
3009                         child_byte_size = field_clang_type.GetByteSize();
3010 
3011                         // Figure out the field offset within the current struct/union/class type
3012                         bit_offset = record_layout.getFieldOffset (field_idx);
3013                         child_byte_offset = bit_offset / 8;
3014                         if (ClangASTContext::FieldIsBitfield (m_ast, *field, child_bitfield_bit_size))
3015                             child_bitfield_bit_offset = bit_offset % 8;
3016 
3017                         return field_clang_type;
3018                     }
3019                 }
3020             }
3021             break;
3022 
3023         case clang::Type::ObjCObject:
3024         case clang::Type::ObjCInterface:
3025             if (idx_is_valid && GetCompleteType())
3026             {
3027                 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(parent_qual_type.getTypePtr());
3028                 assert (objc_class_type);
3029                 if (objc_class_type)
3030                 {
3031                     uint32_t child_idx = 0;
3032                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3033 
3034                     if (class_interface_decl)
3035                     {
3036 
3037                         const ASTRecordLayout &interface_layout = m_ast->getASTObjCInterfaceLayout(class_interface_decl);
3038                         ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3039                         if (superclass_interface_decl)
3040                         {
3041                             if (omit_empty_base_classes)
3042                             {
3043                                 ClangASTType base_class_clang_type (m_ast, m_ast->getObjCInterfaceType(superclass_interface_decl));
3044                                 if (base_class_clang_type.GetNumChildren(omit_empty_base_classes) > 0)
3045                                 {
3046                                     if (idx == 0)
3047                                     {
3048                                         QualType ivar_qual_type(m_ast->getObjCInterfaceType(superclass_interface_decl));
3049 
3050 
3051                                         child_name.assign(superclass_interface_decl->getNameAsString().c_str());
3052 
3053                                         std::pair<uint64_t, unsigned> ivar_type_info = m_ast->getTypeInfo(ivar_qual_type.getTypePtr());
3054 
3055                                         child_byte_size = ivar_type_info.first / 8;
3056                                         child_byte_offset = 0;
3057                                         child_is_base_class = true;
3058 
3059                                         return ClangASTType (m_ast, ivar_qual_type);
3060                                     }
3061 
3062                                     ++child_idx;
3063                                 }
3064                             }
3065                             else
3066                                 ++child_idx;
3067                         }
3068 
3069                         const uint32_t superclass_idx = child_idx;
3070 
3071                         if (idx < (child_idx + class_interface_decl->ivar_size()))
3072                         {
3073                             ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3074 
3075                             for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos)
3076                             {
3077                                 if (child_idx == idx)
3078                                 {
3079                                     ObjCIvarDecl* ivar_decl = *ivar_pos;
3080 
3081                                     QualType ivar_qual_type(ivar_decl->getType());
3082 
3083                                     child_name.assign(ivar_decl->getNameAsString().c_str());
3084 
3085                                     std::pair<uint64_t, unsigned> ivar_type_info = m_ast->getTypeInfo(ivar_qual_type.getTypePtr());
3086 
3087                                     child_byte_size = ivar_type_info.first / 8;
3088 
3089                                     // Figure out the field offset within the current struct/union/class type
3090                                     // For ObjC objects, we can't trust the bit offset we get from the Clang AST, since
3091                                     // that doesn't account for the space taken up by unbacked properties, or from
3092                                     // the changing size of base classes that are newer than this class.
3093                                     // So if we have a process around that we can ask about this object, do so.
3094                                     child_byte_offset = LLDB_INVALID_IVAR_OFFSET;
3095                                     Process *process = NULL;
3096                                     if (exe_ctx)
3097                                         process = exe_ctx->GetProcessPtr();
3098                                     if (process)
3099                                     {
3100                                         ObjCLanguageRuntime *objc_runtime = process->GetObjCLanguageRuntime();
3101                                         if (objc_runtime != NULL)
3102                                         {
3103                                             ClangASTType parent_ast_type (m_ast, parent_qual_type);
3104                                             child_byte_offset = objc_runtime->GetByteOffsetForIvar (parent_ast_type, ivar_decl->getNameAsString().c_str());
3105                                         }
3106                                     }
3107 
3108                                     // Setting this to UINT32_MAX to make sure we don't compute it twice...
3109                                     bit_offset = UINT32_MAX;
3110 
3111                                     if (child_byte_offset == LLDB_INVALID_IVAR_OFFSET)
3112                                     {
3113                                         bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
3114                                         child_byte_offset = bit_offset / 8;
3115                                     }
3116 
3117                                     // Note, the ObjC Ivar Byte offset is just that, it doesn't account for the bit offset
3118                                     // of a bitfield within its containing object.  So regardless of where we get the byte
3119                                     // offset from, we still need to get the bit offset for bitfields from the layout.
3120 
3121                                     if (ClangASTContext::FieldIsBitfield (m_ast, ivar_decl, child_bitfield_bit_size))
3122                                     {
3123                                         if (bit_offset == UINT32_MAX)
3124                                             bit_offset = interface_layout.getFieldOffset (child_idx - superclass_idx);
3125 
3126                                         child_bitfield_bit_offset = bit_offset % 8;
3127                                     }
3128                                     return ClangASTType (m_ast, ivar_qual_type);
3129                                 }
3130                                 ++child_idx;
3131                             }
3132                         }
3133                     }
3134                 }
3135             }
3136             break;
3137 
3138         case clang::Type::ObjCObjectPointer:
3139             if (idx_is_valid)
3140             {
3141                 ClangASTType pointee_clang_type (GetPointeeType());
3142 
3143                 if (transparent_pointers && pointee_clang_type.IsAggregateType())
3144                 {
3145                     child_is_deref_of_parent = false;
3146                     bool tmp_child_is_deref_of_parent = false;
3147                     return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
3148                                                                         parent_name,
3149                                                                         idx,
3150                                                                         transparent_pointers,
3151                                                                         omit_empty_base_classes,
3152                                                                         ignore_array_bounds,
3153                                                                         child_name,
3154                                                                         child_byte_size,
3155                                                                         child_byte_offset,
3156                                                                         child_bitfield_bit_size,
3157                                                                         child_bitfield_bit_offset,
3158                                                                         child_is_base_class,
3159                                                                         tmp_child_is_deref_of_parent);
3160                 }
3161                 else
3162                 {
3163                     child_is_deref_of_parent = true;
3164                     if (parent_name)
3165                     {
3166                         child_name.assign(1, '*');
3167                         child_name += parent_name;
3168                     }
3169 
3170                     // We have a pointer to an simple type
3171                     if (idx == 0 && pointee_clang_type.GetCompleteType())
3172                     {
3173                         child_byte_size = pointee_clang_type.GetByteSize();
3174                         child_byte_offset = 0;
3175                         return pointee_clang_type;
3176                     }
3177                 }
3178             }
3179             break;
3180 
3181         case clang::Type::Vector:
3182         case clang::Type::ExtVector:
3183             if (idx_is_valid)
3184             {
3185                 const VectorType *array = cast<VectorType>(parent_qual_type.getTypePtr());
3186                 if (array)
3187                 {
3188                     ClangASTType element_type (m_ast, array->getElementType());
3189                     if (element_type.GetCompleteType())
3190                     {
3191                         char element_name[64];
3192                         ::snprintf (element_name, sizeof (element_name), "[%zu]", idx);
3193                         child_name.assign(element_name);
3194                         child_byte_size = element_type.GetByteSize();
3195                         child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
3196                         return element_type;
3197                     }
3198                 }
3199             }
3200             break;
3201 
3202         case clang::Type::ConstantArray:
3203         case clang::Type::IncompleteArray:
3204             if (ignore_array_bounds || idx_is_valid)
3205             {
3206                 const ArrayType *array = cast<ArrayType>(parent_qual_type.getTypePtr());
3207                 if (array)
3208                 {
3209                     ClangASTType element_type (m_ast, array->getElementType());
3210                     if (element_type.GetCompleteType())
3211                     {
3212                         char element_name[64];
3213                         ::snprintf (element_name, sizeof (element_name), "[%zu]", idx);
3214                         child_name.assign(element_name);
3215                         child_byte_size = element_type.GetByteSize();
3216                         child_byte_offset = (int32_t)idx * (int32_t)child_byte_size;
3217                         return element_type;
3218                     }
3219                 }
3220             }
3221             break;
3222 
3223 
3224         case clang::Type::Pointer:
3225             if (idx_is_valid)
3226             {
3227                 ClangASTType pointee_clang_type (GetPointeeType());
3228 
3229                 // Don't dereference "void *" pointers
3230                 if (pointee_clang_type.IsVoidType())
3231                     return ClangASTType();
3232 
3233                 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
3234                 {
3235                     child_is_deref_of_parent = false;
3236                     bool tmp_child_is_deref_of_parent = false;
3237                     return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
3238                                                                         parent_name,
3239                                                                         idx,
3240                                                                         transparent_pointers,
3241                                                                         omit_empty_base_classes,
3242                                                                         ignore_array_bounds,
3243                                                                         child_name,
3244                                                                         child_byte_size,
3245                                                                         child_byte_offset,
3246                                                                         child_bitfield_bit_size,
3247                                                                         child_bitfield_bit_offset,
3248                                                                         child_is_base_class,
3249                                                                         tmp_child_is_deref_of_parent);
3250                 }
3251                 else
3252                 {
3253                     child_is_deref_of_parent = true;
3254 
3255                     if (parent_name)
3256                     {
3257                         child_name.assign(1, '*');
3258                         child_name += parent_name;
3259                     }
3260 
3261                     // We have a pointer to an simple type
3262                     if (idx == 0)
3263                     {
3264                         child_byte_size = pointee_clang_type.GetByteSize();
3265                         child_byte_offset = 0;
3266                         return pointee_clang_type;
3267                     }
3268                 }
3269             }
3270             break;
3271 
3272         case clang::Type::LValueReference:
3273         case clang::Type::RValueReference:
3274             if (idx_is_valid)
3275             {
3276                 const ReferenceType *reference_type = cast<ReferenceType>(parent_qual_type.getTypePtr());
3277                 ClangASTType pointee_clang_type (m_ast, reference_type->getPointeeType());
3278                 if (transparent_pointers && pointee_clang_type.IsAggregateType ())
3279                 {
3280                     child_is_deref_of_parent = false;
3281                     bool tmp_child_is_deref_of_parent = false;
3282                     return pointee_clang_type.GetChildClangTypeAtIndex (exe_ctx,
3283                                                                         parent_name,
3284                                                                         idx,
3285                                                                         transparent_pointers,
3286                                                                         omit_empty_base_classes,
3287                                                                         ignore_array_bounds,
3288                                                                         child_name,
3289                                                                         child_byte_size,
3290                                                                         child_byte_offset,
3291                                                                         child_bitfield_bit_size,
3292                                                                         child_bitfield_bit_offset,
3293                                                                         child_is_base_class,
3294                                                                         tmp_child_is_deref_of_parent);
3295                 }
3296                 else
3297                 {
3298                     if (parent_name)
3299                     {
3300                         child_name.assign(1, '&');
3301                         child_name += parent_name;
3302                     }
3303 
3304                     // We have a pointer to an simple type
3305                     if (idx == 0)
3306                     {
3307                         child_byte_size = pointee_clang_type.GetByteSize();
3308                         child_byte_offset = 0;
3309                         return pointee_clang_type;
3310                     }
3311                 }
3312             }
3313             break;
3314 
3315         case clang::Type::Typedef:
3316             {
3317                 ClangASTType typedefed_clang_type (m_ast, cast<TypedefType>(parent_qual_type)->getDecl()->getUnderlyingType());
3318                 return typedefed_clang_type.GetChildClangTypeAtIndex (exe_ctx,
3319                                                                       parent_name,
3320                                                                       idx,
3321                                                                       transparent_pointers,
3322                                                                       omit_empty_base_classes,
3323                                                                       ignore_array_bounds,
3324                                                                       child_name,
3325                                                                       child_byte_size,
3326                                                                       child_byte_offset,
3327                                                                       child_bitfield_bit_size,
3328                                                                       child_bitfield_bit_offset,
3329                                                                       child_is_base_class,
3330                                                                       child_is_deref_of_parent);
3331             }
3332             break;
3333 
3334         case clang::Type::Elaborated:
3335             {
3336                 ClangASTType elaborated_clang_type (m_ast, cast<ElaboratedType>(parent_qual_type)->getNamedType());
3337                 return elaborated_clang_type.GetChildClangTypeAtIndex (exe_ctx,
3338                                                                        parent_name,
3339                                                                        idx,
3340                                                                        transparent_pointers,
3341                                                                        omit_empty_base_classes,
3342                                                                        ignore_array_bounds,
3343                                                                        child_name,
3344                                                                        child_byte_size,
3345                                                                        child_byte_offset,
3346                                                                        child_bitfield_bit_size,
3347                                                                        child_bitfield_bit_offset,
3348                                                                        child_is_base_class,
3349                                                                        child_is_deref_of_parent);
3350             }
3351 
3352         case clang::Type::Paren:
3353             {
3354                 ClangASTType paren_clang_type (m_ast, llvm::cast<clang::ParenType>(parent_qual_type)->desugar());
3355                 return paren_clang_type.GetChildClangTypeAtIndex (exe_ctx,
3356                                                                   parent_name,
3357                                                                   idx,
3358                                                                   transparent_pointers,
3359                                                                   omit_empty_base_classes,
3360                                                                   ignore_array_bounds,
3361                                                                   child_name,
3362                                                                   child_byte_size,
3363                                                                   child_byte_offset,
3364                                                                   child_bitfield_bit_size,
3365                                                                   child_bitfield_bit_offset,
3366                                                                   child_is_base_class,
3367                                                                   child_is_deref_of_parent);
3368             }
3369 
3370 
3371         default:
3372             break;
3373     }
3374     return ClangASTType();
3375 }
3376 
3377 static inline bool
BaseSpecifierIsEmpty(const CXXBaseSpecifier * b)3378 BaseSpecifierIsEmpty (const CXXBaseSpecifier *b)
3379 {
3380     return ClangASTContext::RecordHasFields(b->getType()->getAsCXXRecordDecl()) == false;
3381 }
3382 
3383 static uint32_t
GetIndexForRecordBase(const RecordDecl * record_decl,const CXXBaseSpecifier * base_spec,bool omit_empty_base_classes)3384 GetIndexForRecordBase
3385 (
3386  const RecordDecl *record_decl,
3387  const CXXBaseSpecifier *base_spec,
3388  bool omit_empty_base_classes
3389  )
3390 {
3391     uint32_t child_idx = 0;
3392 
3393     const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3394 
3395     //    const char *super_name = record_decl->getNameAsCString();
3396     //    const char *base_name = base_spec->getType()->getAs<RecordType>()->getDecl()->getNameAsCString();
3397     //    printf ("GetIndexForRecordChild (%s, %s)\n", super_name, base_name);
3398     //
3399     if (cxx_record_decl)
3400     {
3401         CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3402         for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3403              base_class != base_class_end;
3404              ++base_class)
3405         {
3406             if (omit_empty_base_classes)
3407             {
3408                 if (BaseSpecifierIsEmpty (base_class))
3409                     continue;
3410             }
3411 
3412             //            printf ("GetIndexForRecordChild (%s, %s) base[%u] = %s\n", super_name, base_name,
3413             //                    child_idx,
3414             //                    base_class->getType()->getAs<RecordType>()->getDecl()->getNameAsCString());
3415             //
3416             //
3417             if (base_class == base_spec)
3418                 return child_idx;
3419             ++child_idx;
3420         }
3421     }
3422 
3423     return UINT32_MAX;
3424 }
3425 
3426 
3427 static uint32_t
GetIndexForRecordChild(const RecordDecl * record_decl,NamedDecl * canonical_decl,bool omit_empty_base_classes)3428 GetIndexForRecordChild (const RecordDecl *record_decl,
3429                         NamedDecl *canonical_decl,
3430                         bool omit_empty_base_classes)
3431 {
3432     uint32_t child_idx = ClangASTContext::GetNumBaseClasses (dyn_cast<CXXRecordDecl>(record_decl),
3433                                                              omit_empty_base_classes);
3434 
3435     RecordDecl::field_iterator field, field_end;
3436     for (field = record_decl->field_begin(), field_end = record_decl->field_end();
3437          field != field_end;
3438          ++field, ++child_idx)
3439     {
3440         if (field->getCanonicalDecl() == canonical_decl)
3441             return child_idx;
3442     }
3443 
3444     return UINT32_MAX;
3445 }
3446 
3447 // Look for a child member (doesn't include base classes, but it does include
3448 // their members) in the type hierarchy. Returns an index path into "clang_type"
3449 // on how to reach the appropriate member.
3450 //
3451 //    class A
3452 //    {
3453 //    public:
3454 //        int m_a;
3455 //        int m_b;
3456 //    };
3457 //
3458 //    class B
3459 //    {
3460 //    };
3461 //
3462 //    class C :
3463 //        public B,
3464 //        public A
3465 //    {
3466 //    };
3467 //
3468 // If we have a clang type that describes "class C", and we wanted to looked
3469 // "m_b" in it:
3470 //
3471 // With omit_empty_base_classes == false we would get an integer array back with:
3472 // { 1,  1 }
3473 // The first index 1 is the child index for "class A" within class C
3474 // The second index 1 is the child index for "m_b" within class A
3475 //
3476 // With omit_empty_base_classes == true we would get an integer array back with:
3477 // { 0,  1 }
3478 // The first index 0 is the child index for "class A" within class C (since class B doesn't have any members it doesn't count)
3479 // The second index 1 is the child index for "m_b" within class A
3480 
3481 size_t
GetIndexOfChildMemberWithName(const char * name,bool omit_empty_base_classes,std::vector<uint32_t> & child_indexes) const3482 ClangASTType::GetIndexOfChildMemberWithName (const char *name,
3483                                              bool omit_empty_base_classes,
3484                                              std::vector<uint32_t>& child_indexes) const
3485 {
3486     if (IsValid() && name && name[0])
3487     {
3488         QualType qual_type(GetCanonicalQualType());
3489         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3490         switch (type_class)
3491         {
3492             case clang::Type::Record:
3493                 if (GetCompleteType ())
3494                 {
3495                     const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3496                     const RecordDecl *record_decl = record_type->getDecl();
3497 
3498                     assert(record_decl);
3499                     uint32_t child_idx = 0;
3500 
3501                     const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3502 
3503                     // Try and find a field that matches NAME
3504                     RecordDecl::field_iterator field, field_end;
3505                     StringRef name_sref(name);
3506                     for (field = record_decl->field_begin(), field_end = record_decl->field_end();
3507                          field != field_end;
3508                          ++field, ++child_idx)
3509                     {
3510                         if (field->getName().equals (name_sref))
3511                         {
3512                             // We have to add on the number of base classes to this index!
3513                             child_indexes.push_back (child_idx + ClangASTContext::GetNumBaseClasses (cxx_record_decl, omit_empty_base_classes));
3514                             return child_indexes.size();
3515                         }
3516                     }
3517 
3518                     if (cxx_record_decl)
3519                     {
3520                         const RecordDecl *parent_record_decl = cxx_record_decl;
3521 
3522                         //printf ("parent = %s\n", parent_record_decl->getNameAsCString());
3523 
3524                         //const Decl *root_cdecl = cxx_record_decl->getCanonicalDecl();
3525                         // Didn't find things easily, lets let clang do its thang...
3526                         IdentifierInfo & ident_ref = m_ast->Idents.get(name_sref);
3527                         DeclarationName decl_name(&ident_ref);
3528 
3529                         CXXBasePaths paths;
3530                         if (cxx_record_decl->lookupInBases(CXXRecordDecl::FindOrdinaryMember,
3531                                                            decl_name.getAsOpaquePtr(),
3532                                                            paths))
3533                         {
3534                             CXXBasePaths::const_paths_iterator path, path_end = paths.end();
3535                             for (path = paths.begin(); path != path_end; ++path)
3536                             {
3537                                 const size_t num_path_elements = path->size();
3538                                 for (size_t e=0; e<num_path_elements; ++e)
3539                                 {
3540                                     CXXBasePathElement elem = (*path)[e];
3541 
3542                                     child_idx = GetIndexForRecordBase (parent_record_decl, elem.Base, omit_empty_base_classes);
3543                                     if (child_idx == UINT32_MAX)
3544                                     {
3545                                         child_indexes.clear();
3546                                         return 0;
3547                                     }
3548                                     else
3549                                     {
3550                                         child_indexes.push_back (child_idx);
3551                                         parent_record_decl = cast<RecordDecl>(elem.Base->getType()->getAs<RecordType>()->getDecl());
3552                                     }
3553                                 }
3554                                 for (NamedDecl *path_decl : path->Decls)
3555                                 {
3556                                     child_idx = GetIndexForRecordChild (parent_record_decl, path_decl, omit_empty_base_classes);
3557                                     if (child_idx == UINT32_MAX)
3558                                     {
3559                                         child_indexes.clear();
3560                                         return 0;
3561                                     }
3562                                     else
3563                                     {
3564                                         child_indexes.push_back (child_idx);
3565                                     }
3566                                 }
3567                             }
3568                             return child_indexes.size();
3569                         }
3570                     }
3571 
3572                 }
3573                 break;
3574 
3575             case clang::Type::ObjCObject:
3576             case clang::Type::ObjCInterface:
3577                 if (GetCompleteType ())
3578                 {
3579                     StringRef name_sref(name);
3580                     const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3581                     assert (objc_class_type);
3582                     if (objc_class_type)
3583                     {
3584                         uint32_t child_idx = 0;
3585                         ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3586 
3587                         if (class_interface_decl)
3588                         {
3589                             ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3590                             ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3591 
3592                             for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
3593                             {
3594                                 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3595 
3596                                 if (ivar_decl->getName().equals (name_sref))
3597                                 {
3598                                     if ((!omit_empty_base_classes && superclass_interface_decl) ||
3599                                         ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
3600                                         ++child_idx;
3601 
3602                                     child_indexes.push_back (child_idx);
3603                                     return child_indexes.size();
3604                                 }
3605                             }
3606 
3607                             if (superclass_interface_decl)
3608                             {
3609                                 // The super class index is always zero for ObjC classes,
3610                                 // so we push it onto the child indexes in case we find
3611                                 // an ivar in our superclass...
3612                                 child_indexes.push_back (0);
3613 
3614                                 ClangASTType superclass_clang_type (m_ast, m_ast->getObjCInterfaceType(superclass_interface_decl));
3615                                 if (superclass_clang_type.GetIndexOfChildMemberWithName (name,
3616                                                                                          omit_empty_base_classes,
3617                                                                                          child_indexes))
3618                                 {
3619                                     // We did find an ivar in a superclass so just
3620                                     // return the results!
3621                                     return child_indexes.size();
3622                                 }
3623 
3624                                 // We didn't find an ivar matching "name" in our
3625                                 // superclass, pop the superclass zero index that
3626                                 // we pushed on above.
3627                                 child_indexes.pop_back();
3628                             }
3629                         }
3630                     }
3631                 }
3632                 break;
3633 
3634             case clang::Type::ObjCObjectPointer:
3635                 {
3636                     ClangASTType objc_object_clang_type (m_ast, cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
3637                     return objc_object_clang_type.GetIndexOfChildMemberWithName (name,
3638                                                                                  omit_empty_base_classes,
3639                                                                                  child_indexes);
3640                 }
3641                 break;
3642 
3643 
3644             case clang::Type::ConstantArray:
3645             {
3646                 //                const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
3647                 //                const uint64_t element_count = array->getSize().getLimitedValue();
3648                 //
3649                 //                if (idx < element_count)
3650                 //                {
3651                 //                    std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
3652                 //
3653                 //                    char element_name[32];
3654                 //                    ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
3655                 //
3656                 //                    child_name.assign(element_name);
3657                 //                    assert(field_type_info.first % 8 == 0);
3658                 //                    child_byte_size = field_type_info.first / 8;
3659                 //                    child_byte_offset = idx * child_byte_size;
3660                 //                    return array->getElementType().getAsOpaquePtr();
3661                 //                }
3662             }
3663                 break;
3664 
3665                 //        case clang::Type::MemberPointerType:
3666                 //            {
3667                 //                MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
3668                 //                QualType pointee_type = mem_ptr_type->getPointeeType();
3669                 //
3670                 //                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3671                 //                {
3672                 //                    return GetIndexOfChildWithName (ast,
3673                 //                                                    mem_ptr_type->getPointeeType().getAsOpaquePtr(),
3674                 //                                                    name);
3675                 //                }
3676                 //            }
3677                 //            break;
3678                 //
3679             case clang::Type::LValueReference:
3680             case clang::Type::RValueReference:
3681                 {
3682                     const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
3683                     QualType pointee_type(reference_type->getPointeeType());
3684                     ClangASTType pointee_clang_type (m_ast, pointee_type);
3685 
3686                     if (pointee_clang_type.IsAggregateType ())
3687                     {
3688                         return pointee_clang_type.GetIndexOfChildMemberWithName (name,
3689                                                                                  omit_empty_base_classes,
3690                                                                                  child_indexes);
3691                     }
3692                 }
3693                 break;
3694 
3695             case clang::Type::Pointer:
3696             {
3697                 ClangASTType pointee_clang_type (GetPointeeType());
3698 
3699                 if (pointee_clang_type.IsAggregateType ())
3700                 {
3701                     return pointee_clang_type.GetIndexOfChildMemberWithName (name,
3702                                                                              omit_empty_base_classes,
3703                                                                              child_indexes);
3704                 }
3705             }
3706                 break;
3707 
3708             case clang::Type::Typedef:
3709                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildMemberWithName (name,
3710                                                                                                                                          omit_empty_base_classes,
3711                                                                                                                                          child_indexes);
3712 
3713             case clang::Type::Elaborated:
3714                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildMemberWithName (name,
3715                                                                                                                             omit_empty_base_classes,
3716                                                                                                                             child_indexes);
3717 
3718             case clang::Type::Paren:
3719                 return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildMemberWithName (name,
3720                                                                                                                          omit_empty_base_classes,
3721                                                                                                                          child_indexes);
3722 
3723             default:
3724                 break;
3725         }
3726     }
3727     return 0;
3728 }
3729 
3730 
3731 // Get the index of the child of "clang_type" whose name matches. This function
3732 // doesn't descend into the children, but only looks one level deep and name
3733 // matches can include base class names.
3734 
3735 uint32_t
GetIndexOfChildWithName(const char * name,bool omit_empty_base_classes) const3736 ClangASTType::GetIndexOfChildWithName (const char *name, bool omit_empty_base_classes) const
3737 {
3738     if (IsValid() && name && name[0])
3739     {
3740         QualType qual_type(GetCanonicalQualType());
3741 
3742         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3743 
3744         switch (type_class)
3745         {
3746             case clang::Type::Record:
3747                 if (GetCompleteType ())
3748                 {
3749                     const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
3750                     const RecordDecl *record_decl = record_type->getDecl();
3751 
3752                     assert(record_decl);
3753                     uint32_t child_idx = 0;
3754 
3755                     const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
3756 
3757                     if (cxx_record_decl)
3758                     {
3759                         CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
3760                         for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
3761                              base_class != base_class_end;
3762                              ++base_class)
3763                         {
3764                             // Skip empty base classes
3765                             CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
3766                             if (omit_empty_base_classes && ClangASTContext::RecordHasFields(base_class_decl) == false)
3767                                 continue;
3768 
3769                             ClangASTType base_class_clang_type (m_ast, base_class->getType());
3770                             std::string base_class_type_name (base_class_clang_type.GetTypeName());
3771                             if (base_class_type_name.compare (name) == 0)
3772                                 return child_idx;
3773                             ++child_idx;
3774                         }
3775                     }
3776 
3777                     // Try and find a field that matches NAME
3778                     RecordDecl::field_iterator field, field_end;
3779                     StringRef name_sref(name);
3780                     for (field = record_decl->field_begin(), field_end = record_decl->field_end();
3781                          field != field_end;
3782                          ++field, ++child_idx)
3783                     {
3784                         if (field->getName().equals (name_sref))
3785                             return child_idx;
3786                     }
3787 
3788                 }
3789                 break;
3790 
3791             case clang::Type::ObjCObject:
3792             case clang::Type::ObjCInterface:
3793                 if (GetCompleteType())
3794                 {
3795                     StringRef name_sref(name);
3796                     const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
3797                     assert (objc_class_type);
3798                     if (objc_class_type)
3799                     {
3800                         uint32_t child_idx = 0;
3801                         ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
3802 
3803                         if (class_interface_decl)
3804                         {
3805                             ObjCInterfaceDecl::ivar_iterator ivar_pos, ivar_end = class_interface_decl->ivar_end();
3806                             ObjCInterfaceDecl *superclass_interface_decl = class_interface_decl->getSuperClass();
3807 
3808                             for (ivar_pos = class_interface_decl->ivar_begin(); ivar_pos != ivar_end; ++ivar_pos, ++child_idx)
3809                             {
3810                                 const ObjCIvarDecl* ivar_decl = *ivar_pos;
3811 
3812                                 if (ivar_decl->getName().equals (name_sref))
3813                                 {
3814                                     if ((!omit_empty_base_classes && superclass_interface_decl) ||
3815                                         ( omit_empty_base_classes && ObjCDeclHasIVars (superclass_interface_decl, true)))
3816                                         ++child_idx;
3817 
3818                                     return child_idx;
3819                                 }
3820                             }
3821 
3822                             if (superclass_interface_decl)
3823                             {
3824                                 if (superclass_interface_decl->getName().equals (name_sref))
3825                                     return 0;
3826                             }
3827                         }
3828                     }
3829                 }
3830                 break;
3831 
3832             case clang::Type::ObjCObjectPointer:
3833                 {
3834                     ClangASTType pointee_clang_type (m_ast, cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType());
3835                     return pointee_clang_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
3836                 }
3837                 break;
3838 
3839             case clang::Type::ConstantArray:
3840             {
3841                 //                const ConstantArrayType *array = cast<ConstantArrayType>(parent_qual_type.getTypePtr());
3842                 //                const uint64_t element_count = array->getSize().getLimitedValue();
3843                 //
3844                 //                if (idx < element_count)
3845                 //                {
3846                 //                    std::pair<uint64_t, unsigned> field_type_info = ast->getTypeInfo(array->getElementType());
3847                 //
3848                 //                    char element_name[32];
3849                 //                    ::snprintf (element_name, sizeof (element_name), "%s[%u]", parent_name ? parent_name : "", idx);
3850                 //
3851                 //                    child_name.assign(element_name);
3852                 //                    assert(field_type_info.first % 8 == 0);
3853                 //                    child_byte_size = field_type_info.first / 8;
3854                 //                    child_byte_offset = idx * child_byte_size;
3855                 //                    return array->getElementType().getAsOpaquePtr();
3856                 //                }
3857             }
3858                 break;
3859 
3860                 //        case clang::Type::MemberPointerType:
3861                 //            {
3862                 //                MemberPointerType *mem_ptr_type = cast<MemberPointerType>(qual_type.getTypePtr());
3863                 //                QualType pointee_type = mem_ptr_type->getPointeeType();
3864                 //
3865                 //                if (ClangASTContext::IsAggregateType (pointee_type.getAsOpaquePtr()))
3866                 //                {
3867                 //                    return GetIndexOfChildWithName (ast,
3868                 //                                                    mem_ptr_type->getPointeeType().getAsOpaquePtr(),
3869                 //                                                    name);
3870                 //                }
3871                 //            }
3872                 //            break;
3873                 //
3874             case clang::Type::LValueReference:
3875             case clang::Type::RValueReference:
3876                 {
3877                     const ReferenceType *reference_type = cast<ReferenceType>(qual_type.getTypePtr());
3878                     ClangASTType pointee_type (m_ast, reference_type->getPointeeType());
3879 
3880                     if (pointee_type.IsAggregateType ())
3881                     {
3882                         return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
3883                     }
3884                 }
3885                 break;
3886 
3887             case clang::Type::Pointer:
3888                 {
3889                     const PointerType *pointer_type = cast<PointerType>(qual_type.getTypePtr());
3890                     ClangASTType pointee_type (m_ast, pointer_type->getPointeeType());
3891 
3892                     if (pointee_type.IsAggregateType ())
3893                     {
3894                         return pointee_type.GetIndexOfChildWithName (name, omit_empty_base_classes);
3895                     }
3896                     else
3897                     {
3898                         //                    if (parent_name)
3899                         //                    {
3900                         //                        child_name.assign(1, '*');
3901                         //                        child_name += parent_name;
3902                         //                    }
3903                         //
3904                         //                    // We have a pointer to an simple type
3905                         //                    if (idx == 0)
3906                         //                    {
3907                         //                        std::pair<uint64_t, unsigned> clang_type_info = ast->getTypeInfo(pointee_type);
3908                         //                        assert(clang_type_info.first % 8 == 0);
3909                         //                        child_byte_size = clang_type_info.first / 8;
3910                         //                        child_byte_offset = 0;
3911                         //                        return pointee_type.getAsOpaquePtr();
3912                         //                    }
3913                     }
3914                 }
3915                 break;
3916 
3917             case clang::Type::Elaborated:
3918                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
3919 
3920             case clang::Type::Paren:
3921                 return ClangASTType (m_ast, cast<clang::ParenType>(qual_type)->desugar()).GetIndexOfChildWithName (name, omit_empty_base_classes);
3922 
3923             case clang::Type::Typedef:
3924                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetIndexOfChildWithName (name, omit_empty_base_classes);
3925 
3926             default:
3927                 break;
3928         }
3929     }
3930     return UINT32_MAX;
3931 }
3932 
3933 
3934 size_t
GetNumTemplateArguments() const3935 ClangASTType::GetNumTemplateArguments () const
3936 {
3937     if (IsValid())
3938     {
3939         QualType qual_type (GetCanonicalQualType());
3940 
3941         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3942         switch (type_class)
3943         {
3944             case clang::Type::Record:
3945                 if (GetCompleteType ())
3946                 {
3947                     const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3948                     if (cxx_record_decl)
3949                     {
3950                         const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
3951                         if (template_decl)
3952                             return template_decl->getTemplateArgs().size();
3953                     }
3954                 }
3955                 break;
3956 
3957             case clang::Type::Typedef:
3958                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetNumTemplateArguments();
3959 
3960             case clang::Type::Elaborated:
3961                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetNumTemplateArguments();
3962 
3963             case clang::Type::Paren:
3964                 return ClangASTType (m_ast, cast<ParenType>(qual_type)->desugar()).GetNumTemplateArguments();
3965 
3966             default:
3967                 break;
3968         }
3969     }
3970     return 0;
3971 }
3972 
3973 ClangASTType
GetTemplateArgument(size_t arg_idx,lldb::TemplateArgumentKind & kind) const3974 ClangASTType::GetTemplateArgument (size_t arg_idx, lldb::TemplateArgumentKind &kind) const
3975 {
3976     if (IsValid())
3977     {
3978         QualType qual_type (GetCanonicalQualType());
3979 
3980         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
3981         switch (type_class)
3982         {
3983             case clang::Type::Record:
3984                 if (GetCompleteType ())
3985                 {
3986                     const CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
3987                     if (cxx_record_decl)
3988                     {
3989                         const ClassTemplateSpecializationDecl *template_decl = dyn_cast<ClassTemplateSpecializationDecl>(cxx_record_decl);
3990                         if (template_decl && arg_idx < template_decl->getTemplateArgs().size())
3991                         {
3992                             const TemplateArgument &template_arg = template_decl->getTemplateArgs()[arg_idx];
3993                             switch (template_arg.getKind())
3994                             {
3995                                 case clang::TemplateArgument::Null:
3996                                     kind = eTemplateArgumentKindNull;
3997                                     return ClangASTType();
3998 
3999                                 case clang::TemplateArgument::Type:
4000                                     kind = eTemplateArgumentKindType;
4001                                     return ClangASTType(m_ast, template_arg.getAsType());
4002 
4003                                 case clang::TemplateArgument::Declaration:
4004                                     kind = eTemplateArgumentKindDeclaration;
4005                                     return ClangASTType();
4006 
4007                                 case clang::TemplateArgument::Integral:
4008                                     kind = eTemplateArgumentKindIntegral;
4009                                     return ClangASTType(m_ast, template_arg.getIntegralType());
4010 
4011                                 case clang::TemplateArgument::Template:
4012                                     kind = eTemplateArgumentKindTemplate;
4013                                     return ClangASTType();
4014 
4015                                 case clang::TemplateArgument::TemplateExpansion:
4016                                     kind = eTemplateArgumentKindTemplateExpansion;
4017                                     return ClangASTType();
4018 
4019                                 case clang::TemplateArgument::Expression:
4020                                     kind = eTemplateArgumentKindExpression;
4021                                     return ClangASTType();
4022 
4023                                 case clang::TemplateArgument::Pack:
4024                                     kind = eTemplateArgumentKindPack;
4025                                     return ClangASTType();
4026 
4027                                 default:
4028                                     assert (!"Unhandled TemplateArgument::ArgKind");
4029                                     break;
4030                             }
4031                         }
4032                     }
4033                 }
4034                 break;
4035 
4036             case clang::Type::Typedef:
4037                 return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetTemplateArgument (arg_idx, kind);
4038 
4039             case clang::Type::Elaborated:
4040                 return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetTemplateArgument (arg_idx, kind);
4041 
4042             case clang::Type::Paren:
4043                 return ClangASTType (m_ast, cast<ParenType>(qual_type)->desugar()).GetTemplateArgument (arg_idx, kind);
4044 
4045             default:
4046                 break;
4047         }
4048     }
4049     kind = eTemplateArgumentKindNull;
4050     return ClangASTType ();
4051 }
4052 
4053 static bool
IsOperator(const char * name,OverloadedOperatorKind & op_kind)4054 IsOperator (const char *name, OverloadedOperatorKind &op_kind)
4055 {
4056     if (name == NULL || name[0] == '\0')
4057         return false;
4058 
4059 #define OPERATOR_PREFIX "operator"
4060 #define OPERATOR_PREFIX_LENGTH (sizeof (OPERATOR_PREFIX) - 1)
4061 
4062     const char *post_op_name = NULL;
4063 
4064     bool no_space = true;
4065 
4066     if (::strncmp(name, OPERATOR_PREFIX, OPERATOR_PREFIX_LENGTH))
4067         return false;
4068 
4069     post_op_name = name + OPERATOR_PREFIX_LENGTH;
4070 
4071     if (post_op_name[0] == ' ')
4072     {
4073         post_op_name++;
4074         no_space = false;
4075     }
4076 
4077 #undef OPERATOR_PREFIX
4078 #undef OPERATOR_PREFIX_LENGTH
4079 
4080     // This is an operator, set the overloaded operator kind to invalid
4081     // in case this is a conversion operator...
4082     op_kind = NUM_OVERLOADED_OPERATORS;
4083 
4084     switch (post_op_name[0])
4085     {
4086         default:
4087             if (no_space)
4088                 return false;
4089             break;
4090         case 'n':
4091             if (no_space)
4092                 return false;
4093             if  (strcmp (post_op_name, "new") == 0)
4094                 op_kind = OO_New;
4095             else if (strcmp (post_op_name, "new[]") == 0)
4096                 op_kind = OO_Array_New;
4097             break;
4098 
4099         case 'd':
4100             if (no_space)
4101                 return false;
4102             if (strcmp (post_op_name, "delete") == 0)
4103                 op_kind = OO_Delete;
4104             else if (strcmp (post_op_name, "delete[]") == 0)
4105                 op_kind = OO_Array_Delete;
4106             break;
4107 
4108         case '+':
4109             if (post_op_name[1] == '\0')
4110                 op_kind = OO_Plus;
4111             else if (post_op_name[2] == '\0')
4112             {
4113                 if (post_op_name[1] == '=')
4114                     op_kind = OO_PlusEqual;
4115                 else if (post_op_name[1] == '+')
4116                     op_kind = OO_PlusPlus;
4117             }
4118             break;
4119 
4120         case '-':
4121             if (post_op_name[1] == '\0')
4122                 op_kind = OO_Minus;
4123             else if (post_op_name[2] == '\0')
4124             {
4125                 switch (post_op_name[1])
4126                 {
4127                     case '=': op_kind = OO_MinusEqual; break;
4128                     case '-': op_kind = OO_MinusMinus; break;
4129                     case '>': op_kind = OO_Arrow; break;
4130                 }
4131             }
4132             else if (post_op_name[3] == '\0')
4133             {
4134                 if (post_op_name[2] == '*')
4135                     op_kind = OO_ArrowStar; break;
4136             }
4137             break;
4138 
4139         case '*':
4140             if (post_op_name[1] == '\0')
4141                 op_kind = OO_Star;
4142             else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
4143                 op_kind = OO_StarEqual;
4144             break;
4145 
4146         case '/':
4147             if (post_op_name[1] == '\0')
4148                 op_kind = OO_Slash;
4149             else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
4150                 op_kind = OO_SlashEqual;
4151             break;
4152 
4153         case '%':
4154             if (post_op_name[1] == '\0')
4155                 op_kind = OO_Percent;
4156             else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
4157                 op_kind = OO_PercentEqual;
4158             break;
4159 
4160 
4161         case '^':
4162             if (post_op_name[1] == '\0')
4163                 op_kind = OO_Caret;
4164             else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
4165                 op_kind = OO_CaretEqual;
4166             break;
4167 
4168         case '&':
4169             if (post_op_name[1] == '\0')
4170                 op_kind = OO_Amp;
4171             else if (post_op_name[2] == '\0')
4172             {
4173                 switch (post_op_name[1])
4174                 {
4175                     case '=': op_kind = OO_AmpEqual; break;
4176                     case '&': op_kind = OO_AmpAmp; break;
4177                 }
4178             }
4179             break;
4180 
4181         case '|':
4182             if (post_op_name[1] == '\0')
4183                 op_kind = OO_Pipe;
4184             else if (post_op_name[2] == '\0')
4185             {
4186                 switch (post_op_name[1])
4187                 {
4188                     case '=': op_kind = OO_PipeEqual; break;
4189                     case '|': op_kind = OO_PipePipe; break;
4190                 }
4191             }
4192             break;
4193 
4194         case '~':
4195             if (post_op_name[1] == '\0')
4196                 op_kind = OO_Tilde;
4197             break;
4198 
4199         case '!':
4200             if (post_op_name[1] == '\0')
4201                 op_kind = OO_Exclaim;
4202             else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
4203                 op_kind = OO_ExclaimEqual;
4204             break;
4205 
4206         case '=':
4207             if (post_op_name[1] == '\0')
4208                 op_kind = OO_Equal;
4209             else if (post_op_name[1] == '=' && post_op_name[2] == '\0')
4210                 op_kind = OO_EqualEqual;
4211             break;
4212 
4213         case '<':
4214             if (post_op_name[1] == '\0')
4215                 op_kind = OO_Less;
4216             else if (post_op_name[2] == '\0')
4217             {
4218                 switch (post_op_name[1])
4219                 {
4220                     case '<': op_kind = OO_LessLess; break;
4221                     case '=': op_kind = OO_LessEqual; break;
4222                 }
4223             }
4224             else if (post_op_name[3] == '\0')
4225             {
4226                 if (post_op_name[2] == '=')
4227                     op_kind = OO_LessLessEqual;
4228             }
4229             break;
4230 
4231         case '>':
4232             if (post_op_name[1] == '\0')
4233                 op_kind = OO_Greater;
4234             else if (post_op_name[2] == '\0')
4235             {
4236                 switch (post_op_name[1])
4237                 {
4238                     case '>': op_kind = OO_GreaterGreater; break;
4239                     case '=': op_kind = OO_GreaterEqual; break;
4240                 }
4241             }
4242             else if (post_op_name[1] == '>' &&
4243                      post_op_name[2] == '=' &&
4244                      post_op_name[3] == '\0')
4245             {
4246                 op_kind = OO_GreaterGreaterEqual;
4247             }
4248             break;
4249 
4250         case ',':
4251             if (post_op_name[1] == '\0')
4252                 op_kind = OO_Comma;
4253             break;
4254 
4255         case '(':
4256             if (post_op_name[1] == ')' && post_op_name[2] == '\0')
4257                 op_kind = OO_Call;
4258             break;
4259 
4260         case '[':
4261             if (post_op_name[1] == ']' && post_op_name[2] == '\0')
4262                 op_kind = OO_Subscript;
4263             break;
4264     }
4265 
4266     return true;
4267 }
4268 
4269 static inline bool
check_op_param(uint32_t op_kind,bool unary,bool binary,uint32_t num_params)4270 check_op_param (uint32_t op_kind, bool unary, bool binary, uint32_t num_params)
4271 {
4272     // Special-case call since it can take any number of operands
4273     if(op_kind == OO_Call)
4274         return true;
4275 
4276     // The parameter count doens't include "this"
4277     if (num_params == 0)
4278         return unary;
4279     if (num_params == 1)
4280         return binary;
4281     else
4282         return false;
4283 }
4284 
4285 clang::RecordDecl *
GetAsRecordDecl() const4286 ClangASTType::GetAsRecordDecl () const
4287 {
4288     const RecordType *record_type = dyn_cast<RecordType>(GetCanonicalQualType());
4289     if (record_type)
4290         return record_type->getDecl();
4291     return NULL;
4292 }
4293 
4294 clang::CXXRecordDecl *
GetAsCXXRecordDecl() const4295 ClangASTType::GetAsCXXRecordDecl () const
4296 {
4297     return GetCanonicalQualType()->getAsCXXRecordDecl();
4298 }
4299 
4300 ObjCInterfaceDecl *
GetAsObjCInterfaceDecl() const4301 ClangASTType::GetAsObjCInterfaceDecl () const
4302 {
4303     const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(GetCanonicalQualType());
4304     if (objc_class_type)
4305         return objc_class_type->getInterface();
4306     return NULL;
4307 }
4308 
4309 clang::FieldDecl *
AddFieldToRecordType(const char * name,const ClangASTType & field_clang_type,AccessType access,uint32_t bitfield_bit_size)4310 ClangASTType::AddFieldToRecordType (const char *name,
4311                                     const ClangASTType &field_clang_type,
4312                                     AccessType access,
4313                                     uint32_t bitfield_bit_size)
4314 {
4315     if (!IsValid() || !field_clang_type.IsValid())
4316         return NULL;
4317 
4318     FieldDecl *field = NULL;
4319 
4320     clang::Expr *bit_width = NULL;
4321     if (bitfield_bit_size != 0)
4322     {
4323         APInt bitfield_bit_size_apint(m_ast->getTypeSize(m_ast->IntTy), bitfield_bit_size);
4324         bit_width = new (*m_ast)IntegerLiteral (*m_ast, bitfield_bit_size_apint, m_ast->IntTy, SourceLocation());
4325     }
4326 
4327     RecordDecl *record_decl = GetAsRecordDecl ();
4328     if (record_decl)
4329     {
4330         field = FieldDecl::Create (*m_ast,
4331                                    record_decl,
4332                                    SourceLocation(),
4333                                    SourceLocation(),
4334                                    name ? &m_ast->Idents.get(name) : NULL,  // Identifier
4335                                    field_clang_type.GetQualType(),          // Field type
4336                                    NULL,            // TInfo *
4337                                    bit_width,       // BitWidth
4338                                    false,           // Mutable
4339                                    ICIS_NoInit);    // HasInit
4340 
4341         if (!name)
4342         {
4343             // Determine whether this field corresponds to an anonymous
4344             // struct or union.
4345             if (const TagType *TagT = field->getType()->getAs<TagType>()) {
4346                 if (RecordDecl *Rec = dyn_cast<RecordDecl>(TagT->getDecl()))
4347                     if (!Rec->getDeclName()) {
4348                         Rec->setAnonymousStructOrUnion(true);
4349                         field->setImplicit();
4350 
4351                     }
4352             }
4353         }
4354 
4355         if (field)
4356         {
4357             field->setAccess (ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
4358 
4359             record_decl->addDecl(field);
4360 
4361 #ifdef LLDB_CONFIGURATION_DEBUG
4362             VerifyDecl(field);
4363 #endif
4364         }
4365     }
4366     else
4367     {
4368         ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl ();
4369 
4370         if (class_interface_decl)
4371         {
4372             const bool is_synthesized = false;
4373 
4374             field_clang_type.GetCompleteType();
4375 
4376             field = ObjCIvarDecl::Create (*m_ast,
4377                                           class_interface_decl,
4378                                           SourceLocation(),
4379                                           SourceLocation(),
4380                                           name ? &m_ast->Idents.get(name) : NULL,   // Identifier
4381                                           field_clang_type.GetQualType(),           // Field type
4382                                           NULL,                                     // TypeSourceInfo *
4383                                           ConvertAccessTypeToObjCIvarAccessControl (access),
4384                                           bit_width,
4385                                           is_synthesized);
4386 
4387             if (field)
4388             {
4389                 class_interface_decl->addDecl(field);
4390 
4391 #ifdef LLDB_CONFIGURATION_DEBUG
4392                 VerifyDecl(field);
4393 #endif
4394             }
4395         }
4396     }
4397     return field;
4398 }
4399 
4400 void
BuildIndirectFields()4401 ClangASTType::BuildIndirectFields ()
4402 {
4403     RecordDecl *record_decl = GetAsRecordDecl();
4404 
4405     if (!record_decl)
4406         return;
4407 
4408     typedef llvm::SmallVector <IndirectFieldDecl *, 1> IndirectFieldVector;
4409 
4410     IndirectFieldVector indirect_fields;
4411     RecordDecl::field_iterator field_pos;
4412     RecordDecl::field_iterator field_end_pos = record_decl->field_end();
4413     RecordDecl::field_iterator last_field_pos = field_end_pos;
4414     for (field_pos = record_decl->field_begin(); field_pos != field_end_pos; last_field_pos = field_pos++)
4415     {
4416         if (field_pos->isAnonymousStructOrUnion())
4417         {
4418             QualType field_qual_type = field_pos->getType();
4419 
4420             const RecordType *field_record_type = field_qual_type->getAs<RecordType>();
4421 
4422             if (!field_record_type)
4423                 continue;
4424 
4425             RecordDecl *field_record_decl = field_record_type->getDecl();
4426 
4427             if (!field_record_decl)
4428                 continue;
4429 
4430             for (RecordDecl::decl_iterator di = field_record_decl->decls_begin(), de = field_record_decl->decls_end();
4431                  di != de;
4432                  ++di)
4433             {
4434                 if (FieldDecl *nested_field_decl = dyn_cast<FieldDecl>(*di))
4435                 {
4436                     NamedDecl **chain = new (*m_ast) NamedDecl*[2];
4437                     chain[0] = *field_pos;
4438                     chain[1] = nested_field_decl;
4439                     IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*m_ast,
4440                                                                                   record_decl,
4441                                                                                   SourceLocation(),
4442                                                                                   nested_field_decl->getIdentifier(),
4443                                                                                   nested_field_decl->getType(),
4444                                                                                   chain,
4445                                                                                   2);
4446 
4447                     indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
4448                                                                                      nested_field_decl->getAccess()));
4449 
4450                     indirect_fields.push_back(indirect_field);
4451                 }
4452                 else if (IndirectFieldDecl *nested_indirect_field_decl = dyn_cast<IndirectFieldDecl>(*di))
4453                 {
4454                     int nested_chain_size = nested_indirect_field_decl->getChainingSize();
4455                     NamedDecl **chain = new (*m_ast) NamedDecl*[nested_chain_size + 1];
4456                     chain[0] = *field_pos;
4457 
4458                     int chain_index = 1;
4459                     for (IndirectFieldDecl::chain_iterator nci = nested_indirect_field_decl->chain_begin(),
4460                          nce = nested_indirect_field_decl->chain_end();
4461                          nci < nce;
4462                          ++nci)
4463                     {
4464                         chain[chain_index] = *nci;
4465                         chain_index++;
4466                     }
4467 
4468                     IndirectFieldDecl *indirect_field = IndirectFieldDecl::Create(*m_ast,
4469                                                                                   record_decl,
4470                                                                                   SourceLocation(),
4471                                                                                   nested_indirect_field_decl->getIdentifier(),
4472                                                                                   nested_indirect_field_decl->getType(),
4473                                                                                   chain,
4474                                                                                   nested_chain_size + 1);
4475 
4476                     indirect_field->setAccess(ClangASTContext::UnifyAccessSpecifiers(field_pos->getAccess(),
4477                                                                                      nested_indirect_field_decl->getAccess()));
4478 
4479                     indirect_fields.push_back(indirect_field);
4480                 }
4481             }
4482         }
4483     }
4484 
4485     // Check the last field to see if it has an incomplete array type as its
4486     // last member and if it does, the tell the record decl about it
4487     if (last_field_pos != field_end_pos)
4488     {
4489         if (last_field_pos->getType()->isIncompleteArrayType())
4490             record_decl->hasFlexibleArrayMember();
4491     }
4492 
4493     for (IndirectFieldVector::iterator ifi = indirect_fields.begin(), ife = indirect_fields.end();
4494          ifi < ife;
4495          ++ifi)
4496     {
4497         record_decl->addDecl(*ifi);
4498     }
4499 }
4500 
4501 clang::VarDecl *
AddVariableToRecordType(const char * name,const ClangASTType & var_type,AccessType access)4502 ClangASTType::AddVariableToRecordType (const char *name,
4503                                        const ClangASTType &var_type,
4504                                        AccessType access)
4505 {
4506     clang::VarDecl *var_decl = NULL;
4507 
4508     if (!IsValid() || !var_type.IsValid())
4509         return NULL;
4510 
4511     RecordDecl *record_decl = GetAsRecordDecl ();
4512     if (record_decl)
4513     {
4514         var_decl = VarDecl::Create (*m_ast,                                     // ASTContext &
4515                                     record_decl,                                // DeclContext *
4516                                     SourceLocation(),                           // SourceLocation StartLoc
4517                                     SourceLocation(),                           // SourceLocation IdLoc
4518                                     name ? &m_ast->Idents.get(name) : NULL,     // IdentifierInfo *
4519                                     var_type.GetQualType(),                     // Variable QualType
4520                                     NULL,                                       // TypeSourceInfo *
4521                                     SC_Static);                                 // StorageClass
4522         if (var_decl)
4523         {
4524             var_decl->setAccess(ClangASTContext::ConvertAccessTypeToAccessSpecifier (access));
4525             record_decl->addDecl(var_decl);
4526 
4527 #ifdef LLDB_CONFIGURATION_DEBUG
4528             VerifyDecl(var_decl);
4529 #endif
4530         }
4531     }
4532     return var_decl;
4533 }
4534 
4535 
4536 CXXMethodDecl *
AddMethodToCXXRecordType(const char * name,const ClangASTType & method_clang_type,lldb::AccessType access,bool is_virtual,bool is_static,bool is_inline,bool is_explicit,bool is_attr_used,bool is_artificial)4537 ClangASTType::AddMethodToCXXRecordType (const char *name,
4538                                         const ClangASTType &method_clang_type,
4539                                         lldb::AccessType access,
4540                                         bool is_virtual,
4541                                         bool is_static,
4542                                         bool is_inline,
4543                                         bool is_explicit,
4544                                         bool is_attr_used,
4545                                         bool is_artificial)
4546 {
4547     if (!IsValid() || !method_clang_type.IsValid() || name == NULL || name[0] == '\0')
4548         return NULL;
4549 
4550     QualType record_qual_type(GetCanonicalQualType());
4551 
4552     CXXRecordDecl *cxx_record_decl = record_qual_type->getAsCXXRecordDecl();
4553 
4554     if (cxx_record_decl == NULL)
4555         return NULL;
4556 
4557     QualType method_qual_type (method_clang_type.GetQualType());
4558 
4559     CXXMethodDecl *cxx_method_decl = NULL;
4560 
4561     DeclarationName decl_name (&m_ast->Idents.get(name));
4562 
4563     const clang::FunctionType *function_type = dyn_cast<FunctionType>(method_qual_type.getTypePtr());
4564 
4565     if (function_type == NULL)
4566         return NULL;
4567 
4568     const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(function_type));
4569 
4570     if (!method_function_prototype)
4571         return NULL;
4572 
4573     unsigned int num_params = method_function_prototype->getNumArgs();
4574 
4575     CXXDestructorDecl *cxx_dtor_decl(NULL);
4576     CXXConstructorDecl *cxx_ctor_decl(NULL);
4577 
4578     if (is_artificial)
4579         return NULL; // skip everything artificial
4580 
4581     if (name[0] == '~')
4582     {
4583         cxx_dtor_decl = CXXDestructorDecl::Create (*m_ast,
4584                                                    cxx_record_decl,
4585                                                    SourceLocation(),
4586                                                    DeclarationNameInfo (m_ast->DeclarationNames.getCXXDestructorName (m_ast->getCanonicalType (record_qual_type)), SourceLocation()),
4587                                                    method_qual_type,
4588                                                    NULL,
4589                                                    is_inline,
4590                                                    is_artificial);
4591         cxx_method_decl = cxx_dtor_decl;
4592     }
4593     else if (decl_name == cxx_record_decl->getDeclName())
4594     {
4595         cxx_ctor_decl = CXXConstructorDecl::Create (*m_ast,
4596                                                     cxx_record_decl,
4597                                                     SourceLocation(),
4598                                                     DeclarationNameInfo (m_ast->DeclarationNames.getCXXConstructorName (m_ast->getCanonicalType (record_qual_type)), SourceLocation()),
4599                                                     method_qual_type,
4600                                                     NULL, // TypeSourceInfo *
4601                                                     is_explicit,
4602                                                     is_inline,
4603                                                     is_artificial,
4604                                                     false /*is_constexpr*/);
4605         cxx_method_decl = cxx_ctor_decl;
4606     }
4607     else
4608     {
4609         clang::StorageClass SC = is_static ? SC_Static : SC_None;
4610         OverloadedOperatorKind op_kind = NUM_OVERLOADED_OPERATORS;
4611 
4612         if (IsOperator (name, op_kind))
4613         {
4614             if (op_kind != NUM_OVERLOADED_OPERATORS)
4615             {
4616                 // Check the number of operator parameters. Sometimes we have
4617                 // seen bad DWARF that doesn't correctly describe operators and
4618                 // if we try to create a methed and add it to the class, clang
4619                 // will assert and crash, so we need to make sure things are
4620                 // acceptable.
4621                 if (!ClangASTContext::CheckOverloadedOperatorKindParameterCount (op_kind, num_params))
4622                     return NULL;
4623                 cxx_method_decl = CXXMethodDecl::Create (*m_ast,
4624                                                          cxx_record_decl,
4625                                                          SourceLocation(),
4626                                                          DeclarationNameInfo (m_ast->DeclarationNames.getCXXOperatorName (op_kind), SourceLocation()),
4627                                                          method_qual_type,
4628                                                          NULL, // TypeSourceInfo *
4629                                                          SC,
4630                                                          is_inline,
4631                                                          false /*is_constexpr*/,
4632                                                          SourceLocation());
4633             }
4634             else if (num_params == 0)
4635             {
4636                 // Conversion operators don't take params...
4637                 cxx_method_decl = CXXConversionDecl::Create (*m_ast,
4638                                                              cxx_record_decl,
4639                                                              SourceLocation(),
4640                                                              DeclarationNameInfo (m_ast->DeclarationNames.getCXXConversionFunctionName (m_ast->getCanonicalType (function_type->getResultType())), SourceLocation()),
4641                                                              method_qual_type,
4642                                                              NULL, // TypeSourceInfo *
4643                                                              is_inline,
4644                                                              is_explicit,
4645                                                              false /*is_constexpr*/,
4646                                                              SourceLocation());
4647             }
4648         }
4649 
4650         if (cxx_method_decl == NULL)
4651         {
4652             cxx_method_decl = CXXMethodDecl::Create (*m_ast,
4653                                                      cxx_record_decl,
4654                                                      SourceLocation(),
4655                                                      DeclarationNameInfo (decl_name, SourceLocation()),
4656                                                      method_qual_type,
4657                                                      NULL, // TypeSourceInfo *
4658                                                      SC,
4659                                                      is_inline,
4660                                                      false /*is_constexpr*/,
4661                                                      SourceLocation());
4662         }
4663     }
4664 
4665     AccessSpecifier access_specifier = ClangASTContext::ConvertAccessTypeToAccessSpecifier (access);
4666 
4667     cxx_method_decl->setAccess (access_specifier);
4668     cxx_method_decl->setVirtualAsWritten (is_virtual);
4669 
4670     if (is_attr_used)
4671         cxx_method_decl->addAttr(::new (*m_ast) UsedAttr(SourceRange(), *m_ast));
4672 
4673     // Populate the method decl with parameter decls
4674 
4675     llvm::SmallVector<ParmVarDecl *, 12> params;
4676 
4677     for (unsigned param_index = 0;
4678          param_index < num_params;
4679          ++param_index)
4680     {
4681         params.push_back (ParmVarDecl::Create (*m_ast,
4682                                                cxx_method_decl,
4683                                                SourceLocation(),
4684                                                SourceLocation(),
4685                                                NULL, // anonymous
4686                                                method_function_prototype->getArgType(param_index),
4687                                                NULL,
4688                                                SC_None,
4689                                                NULL));
4690     }
4691 
4692     cxx_method_decl->setParams (ArrayRef<ParmVarDecl*>(params));
4693 
4694     cxx_record_decl->addDecl (cxx_method_decl);
4695 
4696     // Sometimes the debug info will mention a constructor (default/copy/move),
4697     // destructor, or assignment operator (copy/move) but there won't be any
4698     // version of this in the code. So we check if the function was artificially
4699     // generated and if it is trivial and this lets the compiler/backend know
4700     // that it can inline the IR for these when it needs to and we can avoid a
4701     // "missing function" error when running expressions.
4702 
4703     if (is_artificial)
4704     {
4705         if (cxx_ctor_decl &&
4706             ((cxx_ctor_decl->isDefaultConstructor() && cxx_record_decl->hasTrivialDefaultConstructor ()) ||
4707              (cxx_ctor_decl->isCopyConstructor()    && cxx_record_decl->hasTrivialCopyConstructor    ()) ||
4708              (cxx_ctor_decl->isMoveConstructor()    && cxx_record_decl->hasTrivialMoveConstructor    ()) ))
4709         {
4710             cxx_ctor_decl->setDefaulted();
4711             cxx_ctor_decl->setTrivial(true);
4712         }
4713         else if (cxx_dtor_decl)
4714         {
4715             if (cxx_record_decl->hasTrivialDestructor())
4716             {
4717                 cxx_dtor_decl->setDefaulted();
4718                 cxx_dtor_decl->setTrivial(true);
4719             }
4720         }
4721         else if ((cxx_method_decl->isCopyAssignmentOperator() && cxx_record_decl->hasTrivialCopyAssignment()) ||
4722                  (cxx_method_decl->isMoveAssignmentOperator() && cxx_record_decl->hasTrivialMoveAssignment()))
4723         {
4724             cxx_method_decl->setDefaulted();
4725             cxx_method_decl->setTrivial(true);
4726         }
4727     }
4728 
4729 #ifdef LLDB_CONFIGURATION_DEBUG
4730     VerifyDecl(cxx_method_decl);
4731 #endif
4732 
4733     //    printf ("decl->isPolymorphic()             = %i\n", cxx_record_decl->isPolymorphic());
4734     //    printf ("decl->isAggregate()               = %i\n", cxx_record_decl->isAggregate());
4735     //    printf ("decl->isPOD()                     = %i\n", cxx_record_decl->isPOD());
4736     //    printf ("decl->isEmpty()                   = %i\n", cxx_record_decl->isEmpty());
4737     //    printf ("decl->isAbstract()                = %i\n", cxx_record_decl->isAbstract());
4738     //    printf ("decl->hasTrivialConstructor()     = %i\n", cxx_record_decl->hasTrivialConstructor());
4739     //    printf ("decl->hasTrivialCopyConstructor() = %i\n", cxx_record_decl->hasTrivialCopyConstructor());
4740     //    printf ("decl->hasTrivialCopyAssignment()  = %i\n", cxx_record_decl->hasTrivialCopyAssignment());
4741     //    printf ("decl->hasTrivialDestructor()      = %i\n", cxx_record_decl->hasTrivialDestructor());
4742     return cxx_method_decl;
4743 }
4744 
4745 
4746 #pragma mark C++ Base Classes
4747 
4748 CXXBaseSpecifier *
CreateBaseClassSpecifier(AccessType access,bool is_virtual,bool base_of_class)4749 ClangASTType::CreateBaseClassSpecifier (AccessType access, bool is_virtual, bool base_of_class)
4750 {
4751     if (IsValid())
4752         return new CXXBaseSpecifier (SourceRange(),
4753                                      is_virtual,
4754                                      base_of_class,
4755                                      ClangASTContext::ConvertAccessTypeToAccessSpecifier (access),
4756                                      m_ast->getTrivialTypeSourceInfo (GetQualType()),
4757                                      SourceLocation());
4758     return NULL;
4759 }
4760 
4761 void
DeleteBaseClassSpecifiers(CXXBaseSpecifier ** base_classes,unsigned num_base_classes)4762 ClangASTType::DeleteBaseClassSpecifiers (CXXBaseSpecifier **base_classes, unsigned num_base_classes)
4763 {
4764     for (unsigned i=0; i<num_base_classes; ++i)
4765     {
4766         delete base_classes[i];
4767         base_classes[i] = NULL;
4768     }
4769 }
4770 
4771 bool
SetBaseClassesForClassType(CXXBaseSpecifier const * const * base_classes,unsigned num_base_classes)4772 ClangASTType::SetBaseClassesForClassType (CXXBaseSpecifier const * const *base_classes,
4773                                           unsigned num_base_classes)
4774 {
4775     if (IsValid())
4776     {
4777         CXXRecordDecl *cxx_record_decl = GetAsCXXRecordDecl();
4778         if (cxx_record_decl)
4779         {
4780             cxx_record_decl->setBases(base_classes, num_base_classes);
4781             return true;
4782         }
4783     }
4784     return false;
4785 }
4786 
4787 bool
SetObjCSuperClass(const ClangASTType & superclass_clang_type)4788 ClangASTType::SetObjCSuperClass (const ClangASTType &superclass_clang_type)
4789 {
4790     if (IsValid() && superclass_clang_type.IsValid())
4791     {
4792         ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl ();
4793         ObjCInterfaceDecl *super_interface_decl = superclass_clang_type.GetAsObjCInterfaceDecl ();
4794         if (class_interface_decl && super_interface_decl)
4795         {
4796             class_interface_decl->setSuperClass(super_interface_decl);
4797             return true;
4798         }
4799     }
4800     return false;
4801 }
4802 
4803 bool
AddObjCClassProperty(const char * property_name,const ClangASTType & property_clang_type,ObjCIvarDecl * ivar_decl,const char * property_setter_name,const char * property_getter_name,uint32_t property_attributes,ClangASTMetadata * metadata)4804 ClangASTType::AddObjCClassProperty (const char *property_name,
4805                                     const ClangASTType &property_clang_type,
4806                                     ObjCIvarDecl *ivar_decl,
4807                                     const char *property_setter_name,
4808                                     const char *property_getter_name,
4809                                     uint32_t property_attributes,
4810                                     ClangASTMetadata *metadata)
4811 {
4812     if (!IsValid() || !property_clang_type.IsValid() || property_name == NULL || property_name[0] == '\0')
4813         return false;
4814 
4815     ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl ();
4816 
4817     if (class_interface_decl)
4818     {
4819         ClangASTType property_clang_type_to_access;
4820 
4821         if (property_clang_type.IsValid())
4822             property_clang_type_to_access = property_clang_type;
4823         else if (ivar_decl)
4824             property_clang_type_to_access = ClangASTType (m_ast, ivar_decl->getType());
4825 
4826         if (class_interface_decl && property_clang_type_to_access.IsValid())
4827         {
4828             clang::TypeSourceInfo *prop_type_source;
4829             if (ivar_decl)
4830                 prop_type_source = m_ast->getTrivialTypeSourceInfo (ivar_decl->getType());
4831             else
4832                 prop_type_source = m_ast->getTrivialTypeSourceInfo (property_clang_type.GetQualType());
4833 
4834             ObjCPropertyDecl *property_decl = ObjCPropertyDecl::Create (*m_ast,
4835                                                                         class_interface_decl,
4836                                                                         SourceLocation(), // Source Location
4837                                                                         &m_ast->Idents.get(property_name),
4838                                                                         SourceLocation(), //Source Location for AT
4839                                                                         SourceLocation(), //Source location for (
4840                                                                         prop_type_source);
4841 
4842             if (property_decl)
4843             {
4844                 if (metadata)
4845                     ClangASTContext::SetMetadata(m_ast, property_decl, *metadata);
4846 
4847                 class_interface_decl->addDecl (property_decl);
4848 
4849                 Selector setter_sel, getter_sel;
4850 
4851                 if (property_setter_name != NULL)
4852                 {
4853                     std::string property_setter_no_colon(property_setter_name, strlen(property_setter_name) - 1);
4854                     clang::IdentifierInfo *setter_ident = &m_ast->Idents.get(property_setter_no_colon.c_str());
4855                     setter_sel = m_ast->Selectors.getSelector(1, &setter_ident);
4856                 }
4857                 else if (!(property_attributes & DW_APPLE_PROPERTY_readonly))
4858                 {
4859                     std::string setter_sel_string("set");
4860                     setter_sel_string.push_back(::toupper(property_name[0]));
4861                     setter_sel_string.append(&property_name[1]);
4862                     clang::IdentifierInfo *setter_ident = &m_ast->Idents.get(setter_sel_string.c_str());
4863                     setter_sel = m_ast->Selectors.getSelector(1, &setter_ident);
4864                 }
4865                 property_decl->setSetterName(setter_sel);
4866                 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_setter);
4867 
4868                 if (property_getter_name != NULL)
4869                 {
4870                     clang::IdentifierInfo *getter_ident = &m_ast->Idents.get(property_getter_name);
4871                     getter_sel = m_ast->Selectors.getSelector(0, &getter_ident);
4872                 }
4873                 else
4874                 {
4875                     clang::IdentifierInfo *getter_ident = &m_ast->Idents.get(property_name);
4876                     getter_sel = m_ast->Selectors.getSelector(0, &getter_ident);
4877                 }
4878                 property_decl->setGetterName(getter_sel);
4879                 property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_getter);
4880 
4881                 if (ivar_decl)
4882                     property_decl->setPropertyIvarDecl (ivar_decl);
4883 
4884                 if (property_attributes & DW_APPLE_PROPERTY_readonly)
4885                     property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readonly);
4886                 if (property_attributes & DW_APPLE_PROPERTY_readwrite)
4887                     property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_readwrite);
4888                 if (property_attributes & DW_APPLE_PROPERTY_assign)
4889                     property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_assign);
4890                 if (property_attributes & DW_APPLE_PROPERTY_retain)
4891                     property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_retain);
4892                 if (property_attributes & DW_APPLE_PROPERTY_copy)
4893                     property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_copy);
4894                 if (property_attributes & DW_APPLE_PROPERTY_nonatomic)
4895                     property_decl->setPropertyAttributes (clang::ObjCPropertyDecl::OBJC_PR_nonatomic);
4896 
4897                 if (!getter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(getter_sel))
4898                 {
4899                     const bool isInstance = true;
4900                     const bool isVariadic = false;
4901                     const bool isSynthesized = false;
4902                     const bool isImplicitlyDeclared = true;
4903                     const bool isDefined = false;
4904                     const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
4905                     const bool HasRelatedResultType = false;
4906 
4907                     ObjCMethodDecl *getter = ObjCMethodDecl::Create (*m_ast,
4908                                                                      SourceLocation(),
4909                                                                      SourceLocation(),
4910                                                                      getter_sel,
4911                                                                      property_clang_type_to_access.GetQualType(),
4912                                                                      NULL,
4913                                                                      class_interface_decl,
4914                                                                      isInstance,
4915                                                                      isVariadic,
4916                                                                      isSynthesized,
4917                                                                      isImplicitlyDeclared,
4918                                                                      isDefined,
4919                                                                      impControl,
4920                                                                      HasRelatedResultType);
4921 
4922                     if (getter && metadata)
4923                         ClangASTContext::SetMetadata(m_ast, getter, *metadata);
4924 
4925                     getter->setMethodParams(*m_ast, ArrayRef<ParmVarDecl*>(), ArrayRef<SourceLocation>());
4926 
4927                     class_interface_decl->addDecl(getter);
4928                 }
4929 
4930                 if (!setter_sel.isNull() && !class_interface_decl->lookupInstanceMethod(setter_sel))
4931                 {
4932                     QualType result_type = m_ast->VoidTy;
4933 
4934                     const bool isInstance = true;
4935                     const bool isVariadic = false;
4936                     const bool isSynthesized = false;
4937                     const bool isImplicitlyDeclared = true;
4938                     const bool isDefined = false;
4939                     const ObjCMethodDecl::ImplementationControl impControl = ObjCMethodDecl::None;
4940                     const bool HasRelatedResultType = false;
4941 
4942                     ObjCMethodDecl *setter = ObjCMethodDecl::Create (*m_ast,
4943                                                                      SourceLocation(),
4944                                                                      SourceLocation(),
4945                                                                      setter_sel,
4946                                                                      result_type,
4947                                                                      NULL,
4948                                                                      class_interface_decl,
4949                                                                      isInstance,
4950                                                                      isVariadic,
4951                                                                      isSynthesized,
4952                                                                      isImplicitlyDeclared,
4953                                                                      isDefined,
4954                                                                      impControl,
4955                                                                      HasRelatedResultType);
4956 
4957                     if (setter && metadata)
4958                         ClangASTContext::SetMetadata(m_ast, setter, *metadata);
4959 
4960                     llvm::SmallVector<ParmVarDecl *, 1> params;
4961 
4962                     params.push_back (ParmVarDecl::Create (*m_ast,
4963                                                            setter,
4964                                                            SourceLocation(),
4965                                                            SourceLocation(),
4966                                                            NULL, // anonymous
4967                                                            property_clang_type_to_access.GetQualType(),
4968                                                            NULL,
4969                                                            SC_Auto,
4970                                                            NULL));
4971 
4972                     setter->setMethodParams(*m_ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
4973 
4974                     class_interface_decl->addDecl(setter);
4975                 }
4976 
4977                 return true;
4978             }
4979         }
4980     }
4981     return false;
4982 }
4983 
4984 bool
IsObjCClassTypeAndHasIVars(bool check_superclass) const4985 ClangASTType::IsObjCClassTypeAndHasIVars (bool check_superclass) const
4986 {
4987     ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl ();
4988     if (class_interface_decl)
4989         return ObjCDeclHasIVars (class_interface_decl, check_superclass);
4990     return false;
4991 }
4992 
4993 
4994 ObjCMethodDecl *
AddMethodToObjCObjectType(const char * name,const ClangASTType & method_clang_type,lldb::AccessType access,bool is_artificial)4995 ClangASTType::AddMethodToObjCObjectType (const char *name,  // the full symbol name as seen in the symbol table ("-[NString stringWithCString:]")
4996                                          const ClangASTType &method_clang_type,
4997                                          lldb::AccessType access,
4998                                          bool is_artificial)
4999 {
5000     if (!IsValid() || !method_clang_type.IsValid())
5001         return NULL;
5002 
5003     ObjCInterfaceDecl *class_interface_decl = GetAsObjCInterfaceDecl();
5004 
5005     if (class_interface_decl == NULL)
5006         return NULL;
5007 
5008     const char *selector_start = ::strchr (name, ' ');
5009     if (selector_start == NULL)
5010         return NULL;
5011 
5012     selector_start++;
5013     llvm::SmallVector<IdentifierInfo *, 12> selector_idents;
5014 
5015     size_t len = 0;
5016     const char *start;
5017     //printf ("name = '%s'\n", name);
5018 
5019     unsigned num_selectors_with_args = 0;
5020     for (start = selector_start;
5021          start && *start != '\0' && *start != ']';
5022          start += len)
5023     {
5024         len = ::strcspn(start, ":]");
5025         bool has_arg = (start[len] == ':');
5026         if (has_arg)
5027             ++num_selectors_with_args;
5028         selector_idents.push_back (&m_ast->Idents.get (StringRef (start, len)));
5029         if (has_arg)
5030             len += 1;
5031     }
5032 
5033 
5034     if (selector_idents.size() == 0)
5035         return 0;
5036 
5037     clang::Selector method_selector = m_ast->Selectors.getSelector (num_selectors_with_args ? selector_idents.size() : 0,
5038                                                                     selector_idents.data());
5039 
5040     QualType method_qual_type (method_clang_type.GetQualType());
5041 
5042     // Populate the method decl with parameter decls
5043     const clang::Type *method_type(method_qual_type.getTypePtr());
5044 
5045     if (method_type == NULL)
5046         return NULL;
5047 
5048     const FunctionProtoType *method_function_prototype (dyn_cast<FunctionProtoType>(method_type));
5049 
5050     if (!method_function_prototype)
5051         return NULL;
5052 
5053 
5054     bool is_variadic = false;
5055     bool is_synthesized = false;
5056     bool is_defined = false;
5057     ObjCMethodDecl::ImplementationControl imp_control = ObjCMethodDecl::None;
5058 
5059     const unsigned num_args = method_function_prototype->getNumArgs();
5060 
5061     if (num_args != num_selectors_with_args)
5062         return NULL; // some debug information is corrupt.  We are not going to deal with it.
5063 
5064     ObjCMethodDecl *objc_method_decl = ObjCMethodDecl::Create (*m_ast,
5065                                                                SourceLocation(), // beginLoc,
5066                                                                SourceLocation(), // endLoc,
5067                                                                method_selector,
5068                                                                method_function_prototype->getResultType(),
5069                                                                NULL, // TypeSourceInfo *ResultTInfo,
5070                                                                GetDeclContextForType (),
5071                                                                name[0] == '-',
5072                                                                is_variadic,
5073                                                                is_synthesized,
5074                                                                true, // is_implicitly_declared; we force this to true because we don't have source locations
5075                                                                is_defined,
5076                                                                imp_control,
5077                                                                false /*has_related_result_type*/);
5078 
5079 
5080     if (objc_method_decl == NULL)
5081         return NULL;
5082 
5083     if (num_args > 0)
5084     {
5085         llvm::SmallVector<ParmVarDecl *, 12> params;
5086 
5087         for (unsigned param_index = 0; param_index < num_args; ++param_index)
5088         {
5089             params.push_back (ParmVarDecl::Create (*m_ast,
5090                                                    objc_method_decl,
5091                                                    SourceLocation(),
5092                                                    SourceLocation(),
5093                                                    NULL, // anonymous
5094                                                    method_function_prototype->getArgType(param_index),
5095                                                    NULL,
5096                                                    SC_Auto,
5097                                                    NULL));
5098         }
5099 
5100         objc_method_decl->setMethodParams(*m_ast, ArrayRef<ParmVarDecl*>(params), ArrayRef<SourceLocation>());
5101     }
5102 
5103     class_interface_decl->addDecl (objc_method_decl);
5104 
5105 #ifdef LLDB_CONFIGURATION_DEBUG
5106     VerifyDecl(objc_method_decl);
5107 #endif
5108 
5109     return objc_method_decl;
5110 }
5111 
5112 
5113 clang::DeclContext *
GetDeclContextForType() const5114 ClangASTType::GetDeclContextForType () const
5115 {
5116     if (!IsValid())
5117         return NULL;
5118 
5119     QualType qual_type(GetCanonicalQualType());
5120     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5121     switch (type_class)
5122     {
5123         case clang::Type::UnaryTransform:           break;
5124         case clang::Type::FunctionNoProto:          break;
5125         case clang::Type::FunctionProto:            break;
5126         case clang::Type::IncompleteArray:          break;
5127         case clang::Type::VariableArray:            break;
5128         case clang::Type::ConstantArray:            break;
5129         case clang::Type::DependentSizedArray:      break;
5130         case clang::Type::ExtVector:                break;
5131         case clang::Type::DependentSizedExtVector:  break;
5132         case clang::Type::Vector:                   break;
5133         case clang::Type::Builtin:                  break;
5134         case clang::Type::BlockPointer:             break;
5135         case clang::Type::Pointer:                  break;
5136         case clang::Type::LValueReference:          break;
5137         case clang::Type::RValueReference:          break;
5138         case clang::Type::MemberPointer:            break;
5139         case clang::Type::Complex:                  break;
5140         case clang::Type::ObjCObject:               break;
5141         case clang::Type::ObjCInterface:            return cast<ObjCObjectType>(qual_type.getTypePtr())->getInterface();
5142         case clang::Type::ObjCObjectPointer:        return ClangASTType (m_ast, cast<ObjCObjectPointerType>(qual_type.getTypePtr())->getPointeeType()).GetDeclContextForType();
5143         case clang::Type::Record:                   return cast<RecordType>(qual_type)->getDecl();
5144         case clang::Type::Enum:                     return cast<EnumType>(qual_type)->getDecl();
5145         case clang::Type::Typedef:                  return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).GetDeclContextForType();
5146         case clang::Type::Elaborated:               return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).GetDeclContextForType();
5147         case clang::Type::Paren:                    return ClangASTType (m_ast, cast<ParenType>(qual_type)->desugar()).GetDeclContextForType();
5148         case clang::Type::TypeOfExpr:               break;
5149         case clang::Type::TypeOf:                   break;
5150         case clang::Type::Decltype:                 break;
5151             //case clang::Type::QualifiedName:          break;
5152         case clang::Type::TemplateSpecialization:   break;
5153         case clang::Type::DependentTemplateSpecialization:  break;
5154         case clang::Type::TemplateTypeParm:         break;
5155         case clang::Type::SubstTemplateTypeParm:    break;
5156         case clang::Type::SubstTemplateTypeParmPack:break;
5157         case clang::Type::PackExpansion:            break;
5158         case clang::Type::UnresolvedUsing:          break;
5159         case clang::Type::Attributed:               break;
5160         case clang::Type::Auto:                     break;
5161         case clang::Type::InjectedClassName:        break;
5162         case clang::Type::DependentName:            break;
5163         case clang::Type::Atomic:                   break;
5164     }
5165     // No DeclContext in this type...
5166     return NULL;
5167 }
5168 
5169 bool
SetDefaultAccessForRecordFields(int default_accessibility,int * assigned_accessibilities,size_t num_assigned_accessibilities)5170 ClangASTType::SetDefaultAccessForRecordFields (int default_accessibility,
5171                                                int *assigned_accessibilities,
5172                                                size_t num_assigned_accessibilities)
5173 {
5174     if (IsValid())
5175     {
5176         RecordDecl *record_decl = GetAsRecordDecl();
5177         if (record_decl)
5178         {
5179             uint32_t field_idx;
5180             RecordDecl::field_iterator field, field_end;
5181             for (field = record_decl->field_begin(), field_end = record_decl->field_end(), field_idx = 0;
5182                  field != field_end;
5183                  ++field, ++field_idx)
5184             {
5185                 // If no accessibility was assigned, assign the correct one
5186                 if (field_idx < num_assigned_accessibilities && assigned_accessibilities[field_idx] == clang::AS_none)
5187                     field->setAccess ((AccessSpecifier)default_accessibility);
5188             }
5189             return true;
5190         }
5191     }
5192     return false;
5193 }
5194 
5195 
5196 bool
SetHasExternalStorage(bool has_extern)5197 ClangASTType::SetHasExternalStorage (bool has_extern)
5198 {
5199     if (!IsValid())
5200         return false;
5201 
5202     QualType qual_type (GetCanonicalQualType());
5203 
5204     const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5205     switch (type_class)
5206     {
5207         case clang::Type::Record:
5208         {
5209             CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5210             if (cxx_record_decl)
5211             {
5212                 cxx_record_decl->setHasExternalLexicalStorage (has_extern);
5213                 cxx_record_decl->setHasExternalVisibleStorage (has_extern);
5214                 return true;
5215             }
5216         }
5217             break;
5218 
5219         case clang::Type::Enum:
5220         {
5221             EnumDecl *enum_decl = cast<EnumType>(qual_type)->getDecl();
5222             if (enum_decl)
5223             {
5224                 enum_decl->setHasExternalLexicalStorage (has_extern);
5225                 enum_decl->setHasExternalVisibleStorage (has_extern);
5226                 return true;
5227             }
5228         }
5229             break;
5230 
5231         case clang::Type::ObjCObject:
5232         case clang::Type::ObjCInterface:
5233         {
5234             const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
5235             assert (objc_class_type);
5236             if (objc_class_type)
5237             {
5238                 ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
5239 
5240                 if (class_interface_decl)
5241                 {
5242                     class_interface_decl->setHasExternalLexicalStorage (has_extern);
5243                     class_interface_decl->setHasExternalVisibleStorage (has_extern);
5244                     return true;
5245                 }
5246             }
5247         }
5248             break;
5249 
5250         case clang::Type::Typedef:
5251             return ClangASTType (m_ast, cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType()).SetHasExternalStorage (has_extern);
5252 
5253         case clang::Type::Elaborated:
5254             return ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).SetHasExternalStorage (has_extern);
5255 
5256         case clang::Type::Paren:
5257             return ClangASTType (m_ast, cast<ParenType>(qual_type)->desugar()).SetHasExternalStorage (has_extern);
5258 
5259         default:
5260             break;
5261     }
5262     return false;
5263 }
5264 
5265 bool
SetTagTypeKind(int kind) const5266 ClangASTType::SetTagTypeKind (int kind) const
5267 {
5268     if (IsValid())
5269     {
5270         QualType tag_qual_type(GetQualType());
5271         const clang::Type *clang_type = tag_qual_type.getTypePtr();
5272         if (clang_type)
5273         {
5274             const TagType *tag_type = dyn_cast<TagType>(clang_type);
5275             if (tag_type)
5276             {
5277                 TagDecl *tag_decl = dyn_cast<TagDecl>(tag_type->getDecl());
5278                 if (tag_decl)
5279                 {
5280                     tag_decl->setTagKind ((TagDecl::TagKind)kind);
5281                     return true;
5282                 }
5283             }
5284         }
5285     }
5286     return false;
5287 }
5288 
5289 
5290 #pragma mark TagDecl
5291 
5292 bool
StartTagDeclarationDefinition()5293 ClangASTType::StartTagDeclarationDefinition ()
5294 {
5295     if (IsValid())
5296     {
5297         QualType qual_type (GetQualType());
5298         const clang::Type *t = qual_type.getTypePtr();
5299         if (t)
5300         {
5301             const TagType *tag_type = dyn_cast<TagType>(t);
5302             if (tag_type)
5303             {
5304                 TagDecl *tag_decl = tag_type->getDecl();
5305                 if (tag_decl)
5306                 {
5307                     tag_decl->startDefinition();
5308                     return true;
5309                 }
5310             }
5311 
5312             const ObjCObjectType *object_type = dyn_cast<ObjCObjectType>(t);
5313             if (object_type)
5314             {
5315                 ObjCInterfaceDecl *interface_decl = object_type->getInterface();
5316                 if (interface_decl)
5317                 {
5318                     interface_decl->startDefinition();
5319                     return true;
5320                 }
5321             }
5322         }
5323     }
5324     return false;
5325 }
5326 
5327 bool
CompleteTagDeclarationDefinition()5328 ClangASTType::CompleteTagDeclarationDefinition ()
5329 {
5330     if (IsValid())
5331     {
5332         QualType qual_type (GetQualType());
5333 
5334         CXXRecordDecl *cxx_record_decl = qual_type->getAsCXXRecordDecl();
5335 
5336         if (cxx_record_decl)
5337         {
5338             cxx_record_decl->completeDefinition();
5339 
5340             return true;
5341         }
5342 
5343         const EnumType *enum_type = dyn_cast<EnumType>(qual_type.getTypePtr());
5344 
5345         if (enum_type)
5346         {
5347             EnumDecl *enum_decl = enum_type->getDecl();
5348 
5349             if (enum_decl)
5350             {
5351                 /// TODO This really needs to be fixed.
5352 
5353                 unsigned NumPositiveBits = 1;
5354                 unsigned NumNegativeBits = 0;
5355 
5356                 QualType promotion_qual_type;
5357                 // If the enum integer type is less than an integer in bit width,
5358                 // then we must promote it to an integer size.
5359                 if (m_ast->getTypeSize(enum_decl->getIntegerType()) < m_ast->getTypeSize(m_ast->IntTy))
5360                 {
5361                     if (enum_decl->getIntegerType()->isSignedIntegerType())
5362                         promotion_qual_type = m_ast->IntTy;
5363                     else
5364                         promotion_qual_type = m_ast->UnsignedIntTy;
5365                 }
5366                 else
5367                     promotion_qual_type = enum_decl->getIntegerType();
5368 
5369                 enum_decl->completeDefinition(enum_decl->getIntegerType(), promotion_qual_type, NumPositiveBits, NumNegativeBits);
5370                 return true;
5371             }
5372         }
5373     }
5374     return false;
5375 }
5376 
5377 
5378 
5379 
5380 
5381 
5382 
5383 bool
AddEnumerationValueToEnumerationType(const ClangASTType & enumerator_clang_type,const Declaration & decl,const char * name,int64_t enum_value,uint32_t enum_value_bit_size)5384 ClangASTType::AddEnumerationValueToEnumerationType (const ClangASTType &enumerator_clang_type,
5385                                                     const Declaration &decl,
5386                                                     const char *name,
5387                                                     int64_t enum_value,
5388                                                     uint32_t enum_value_bit_size)
5389 {
5390     if (IsValid() && enumerator_clang_type.IsValid() && name && name[0])
5391     {
5392         QualType enum_qual_type (GetCanonicalQualType());
5393 
5394         bool is_signed = false;
5395         enumerator_clang_type.IsIntegerType (is_signed);
5396         const clang::Type *clang_type = enum_qual_type.getTypePtr();
5397         if (clang_type)
5398         {
5399             const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5400 
5401             if (enum_type)
5402             {
5403                 llvm::APSInt enum_llvm_apsint(enum_value_bit_size, is_signed);
5404                 enum_llvm_apsint = enum_value;
5405                 EnumConstantDecl *enumerator_decl =
5406                 EnumConstantDecl::Create (*m_ast,
5407                                           enum_type->getDecl(),
5408                                           SourceLocation(),
5409                                           name ? &m_ast->Idents.get(name) : NULL,    // Identifier
5410                                           enumerator_clang_type.GetQualType(),
5411                                           NULL,
5412                                           enum_llvm_apsint);
5413 
5414                 if (enumerator_decl)
5415                 {
5416                     enum_type->getDecl()->addDecl(enumerator_decl);
5417 
5418 #ifdef LLDB_CONFIGURATION_DEBUG
5419                     VerifyDecl(enumerator_decl);
5420 #endif
5421 
5422                     return true;
5423                 }
5424             }
5425         }
5426     }
5427     return false;
5428 }
5429 
5430 
5431 ClangASTType
GetEnumerationIntegerType() const5432 ClangASTType::GetEnumerationIntegerType () const
5433 {
5434     QualType enum_qual_type (GetCanonicalQualType());
5435     const clang::Type *clang_type = enum_qual_type.getTypePtr();
5436     if (clang_type)
5437     {
5438         const EnumType *enum_type = dyn_cast<EnumType>(clang_type);
5439         if (enum_type)
5440         {
5441             EnumDecl *enum_decl = enum_type->getDecl();
5442             if (enum_decl)
5443                 return ClangASTType (m_ast, enum_decl->getIntegerType());
5444         }
5445     }
5446     return ClangASTType();
5447 }
5448 
5449 ClangASTType
CreateMemberPointerType(const ClangASTType & pointee_type) const5450 ClangASTType::CreateMemberPointerType (const ClangASTType &pointee_type) const
5451 {
5452     if (IsValid() && pointee_type.IsValid())
5453     {
5454         return ClangASTType (m_ast, m_ast->getMemberPointerType (pointee_type.GetQualType(),
5455                                                                  GetQualType().getTypePtr()));
5456     }
5457     return ClangASTType();
5458 }
5459 
5460 
5461 size_t
ConvertStringToFloatValue(const char * s,uint8_t * dst,size_t dst_size) const5462 ClangASTType::ConvertStringToFloatValue (const char *s, uint8_t *dst, size_t dst_size) const
5463 {
5464     if (IsValid())
5465     {
5466         QualType qual_type (GetCanonicalQualType());
5467         uint32_t count = 0;
5468         bool is_complex = false;
5469         if (IsFloatingPointType (count, is_complex))
5470         {
5471             // TODO: handle complex and vector types
5472             if (count != 1)
5473                 return false;
5474 
5475             StringRef s_sref(s);
5476             APFloat ap_float(m_ast->getFloatTypeSemantics(qual_type), s_sref);
5477 
5478             const uint64_t bit_size = m_ast->getTypeSize (qual_type);
5479             const uint64_t byte_size = bit_size / 8;
5480             if (dst_size >= byte_size)
5481             {
5482                 if (bit_size == sizeof(float)*8)
5483                 {
5484                     float float32 = ap_float.convertToFloat();
5485                     ::memcpy (dst, &float32, byte_size);
5486                     return byte_size;
5487                 }
5488                 else if (bit_size >= 64)
5489                 {
5490                     llvm::APInt ap_int(ap_float.bitcastToAPInt());
5491                     ::memcpy (dst, ap_int.getRawData(), byte_size);
5492                     return byte_size;
5493                 }
5494             }
5495         }
5496     }
5497     return 0;
5498 }
5499 
5500 
5501 
5502 //----------------------------------------------------------------------
5503 // Dumping types
5504 //----------------------------------------------------------------------
5505 #define DEPTH_INCREMENT 2
5506 
5507 void
DumpValue(ExecutionContext * exe_ctx,Stream * s,lldb::Format format,const lldb_private::DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset,bool show_types,bool show_summary,bool verbose,uint32_t depth)5508 ClangASTType::DumpValue (ExecutionContext *exe_ctx,
5509                          Stream *s,
5510                          lldb::Format format,
5511                          const lldb_private::DataExtractor &data,
5512                          lldb::offset_t data_byte_offset,
5513                          size_t data_byte_size,
5514                          uint32_t bitfield_bit_size,
5515                          uint32_t bitfield_bit_offset,
5516                          bool show_types,
5517                          bool show_summary,
5518                          bool verbose,
5519                          uint32_t depth)
5520 {
5521     if (!IsValid())
5522         return;
5523 
5524     QualType qual_type(GetQualType());
5525     switch (qual_type->getTypeClass())
5526     {
5527     case clang::Type::Record:
5528         if (GetCompleteType ())
5529         {
5530             const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
5531             const RecordDecl *record_decl = record_type->getDecl();
5532             assert(record_decl);
5533             uint32_t field_bit_offset = 0;
5534             uint32_t field_byte_offset = 0;
5535             const ASTRecordLayout &record_layout = m_ast->getASTRecordLayout(record_decl);
5536             uint32_t child_idx = 0;
5537 
5538             const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
5539             if (cxx_record_decl)
5540             {
5541                 // We might have base classes to print out first
5542                 CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
5543                 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
5544                      base_class != base_class_end;
5545                      ++base_class)
5546                 {
5547                     const CXXRecordDecl *base_class_decl = cast<CXXRecordDecl>(base_class->getType()->getAs<RecordType>()->getDecl());
5548 
5549                     // Skip empty base classes
5550                     if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
5551                         continue;
5552 
5553                     if (base_class->isVirtual())
5554                         field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl).getQuantity() * 8;
5555                     else
5556                         field_bit_offset = record_layout.getBaseClassOffset(base_class_decl).getQuantity() * 8;
5557                     field_byte_offset = field_bit_offset / 8;
5558                     assert (field_bit_offset % 8 == 0);
5559                     if (child_idx == 0)
5560                         s->PutChar('{');
5561                     else
5562                         s->PutChar(',');
5563 
5564                     QualType base_class_qual_type = base_class->getType();
5565                     std::string base_class_type_name(base_class_qual_type.getAsString());
5566 
5567                     // Indent and print the base class type name
5568                     s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
5569 
5570                     std::pair<uint64_t, unsigned> base_class_type_info = m_ast->getTypeInfo(base_class_qual_type);
5571 
5572                     // Dump the value of the member
5573                     ClangASTType base_clang_type(m_ast, base_class_qual_type);
5574                     base_clang_type.DumpValue (exe_ctx,
5575                                                s,                                   // Stream to dump to
5576                                                base_clang_type.GetFormat(),         // The format with which to display the member
5577                                                data,                                // Data buffer containing all bytes for this type
5578                                                data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
5579                                                base_class_type_info.first / 8,      // Size of this type in bytes
5580                                                0,                                   // Bitfield bit size
5581                                                0,                                   // Bitfield bit offset
5582                                                show_types,                          // Boolean indicating if we should show the variable types
5583                                                show_summary,                        // Boolean indicating if we should show a summary for the current type
5584                                                verbose,                             // Verbose output?
5585                                                depth + DEPTH_INCREMENT);            // Scope depth for any types that have children
5586 
5587                     ++child_idx;
5588                 }
5589             }
5590             uint32_t field_idx = 0;
5591             RecordDecl::field_iterator field, field_end;
5592             for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
5593             {
5594                 // Print the starting squiggly bracket (if this is the
5595                 // first member) or comman (for member 2 and beyong) for
5596                 // the struct/union/class member.
5597                 if (child_idx == 0)
5598                     s->PutChar('{');
5599                 else
5600                     s->PutChar(',');
5601 
5602                 // Indent
5603                 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
5604 
5605                 QualType field_type = field->getType();
5606                 // Print the member type if requested
5607                 // Figure out the type byte size (field_type_info.first) and
5608                 // alignment (field_type_info.second) from the AST context.
5609                 std::pair<uint64_t, unsigned> field_type_info = m_ast->getTypeInfo(field_type);
5610                 assert(field_idx < record_layout.getFieldCount());
5611                 // Figure out the field offset within the current struct/union/class type
5612                 field_bit_offset = record_layout.getFieldOffset (field_idx);
5613                 field_byte_offset = field_bit_offset / 8;
5614                 uint32_t field_bitfield_bit_size = 0;
5615                 uint32_t field_bitfield_bit_offset = 0;
5616                 if (ClangASTContext::FieldIsBitfield (m_ast, *field, field_bitfield_bit_size))
5617                     field_bitfield_bit_offset = field_bit_offset % 8;
5618 
5619                 if (show_types)
5620                 {
5621                     std::string field_type_name(field_type.getAsString());
5622                     if (field_bitfield_bit_size > 0)
5623                         s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
5624                     else
5625                         s->Printf("(%s) ", field_type_name.c_str());
5626                 }
5627                 // Print the member name and equal sign
5628                 s->Printf("%s = ", field->getNameAsString().c_str());
5629 
5630 
5631                 // Dump the value of the member
5632                 ClangASTType field_clang_type (m_ast, field_type);
5633                 field_clang_type.DumpValue (exe_ctx,
5634                                             s,                              // Stream to dump to
5635                                             field_clang_type.GetFormat(),   // The format with which to display the member
5636                                             data,                           // Data buffer containing all bytes for this type
5637                                             data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
5638                                             field_type_info.first / 8,      // Size of this type in bytes
5639                                             field_bitfield_bit_size,        // Bitfield bit size
5640                                             field_bitfield_bit_offset,      // Bitfield bit offset
5641                                             show_types,                     // Boolean indicating if we should show the variable types
5642                                             show_summary,                   // Boolean indicating if we should show a summary for the current type
5643                                             verbose,                        // Verbose output?
5644                                             depth + DEPTH_INCREMENT);       // Scope depth for any types that have children
5645             }
5646 
5647             // Indent the trailing squiggly bracket
5648             if (child_idx > 0)
5649                 s->Printf("\n%*s}", depth, "");
5650         }
5651         return;
5652 
5653     case clang::Type::Enum:
5654         if (GetCompleteType ())
5655         {
5656             const EnumType *enum_type = cast<EnumType>(qual_type.getTypePtr());
5657             const EnumDecl *enum_decl = enum_type->getDecl();
5658             assert(enum_decl);
5659             EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5660             lldb::offset_t offset = data_byte_offset;
5661             const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
5662             for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
5663             {
5664                 if (enum_pos->getInitVal() == enum_value)
5665                 {
5666                     s->Printf("%s", enum_pos->getNameAsString().c_str());
5667                     return;
5668                 }
5669             }
5670             // If we have gotten here we didn't get find the enumerator in the
5671             // enum decl, so just print the integer.
5672             s->Printf("%" PRIi64, enum_value);
5673         }
5674         return;
5675 
5676     case clang::Type::ConstantArray:
5677         {
5678             const ConstantArrayType *array = cast<ConstantArrayType>(qual_type.getTypePtr());
5679             bool is_array_of_characters = false;
5680             QualType element_qual_type = array->getElementType();
5681 
5682             const clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
5683             if (canonical_type)
5684                 is_array_of_characters = canonical_type->isCharType();
5685 
5686             const uint64_t element_count = array->getSize().getLimitedValue();
5687 
5688             std::pair<uint64_t, unsigned> field_type_info = m_ast->getTypeInfo(element_qual_type);
5689 
5690             uint32_t element_idx = 0;
5691             uint32_t element_offset = 0;
5692             uint64_t element_byte_size = field_type_info.first / 8;
5693             uint32_t element_stride = element_byte_size;
5694 
5695             if (is_array_of_characters)
5696             {
5697                 s->PutChar('"');
5698                 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
5699                 s->PutChar('"');
5700                 return;
5701             }
5702             else
5703             {
5704                 ClangASTType element_clang_type(m_ast, element_qual_type);
5705                 lldb::Format element_format = element_clang_type.GetFormat();
5706 
5707                 for (element_idx = 0; element_idx < element_count; ++element_idx)
5708                 {
5709                     // Print the starting squiggly bracket (if this is the
5710                     // first member) or comman (for member 2 and beyong) for
5711                     // the struct/union/class member.
5712                     if (element_idx == 0)
5713                         s->PutChar('{');
5714                     else
5715                         s->PutChar(',');
5716 
5717                     // Indent and print the index
5718                     s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
5719 
5720                     // Figure out the field offset within the current struct/union/class type
5721                     element_offset = element_idx * element_stride;
5722 
5723                     // Dump the value of the member
5724                     element_clang_type.DumpValue (exe_ctx,
5725                                                   s,                              // Stream to dump to
5726                                                   element_format,                 // The format with which to display the element
5727                                                   data,                           // Data buffer containing all bytes for this type
5728                                                   data_byte_offset + element_offset,// Offset into "data" where to grab value from
5729                                                   element_byte_size,              // Size of this type in bytes
5730                                                   0,                              // Bitfield bit size
5731                                                   0,                              // Bitfield bit offset
5732                                                   show_types,                     // Boolean indicating if we should show the variable types
5733                                                   show_summary,                   // Boolean indicating if we should show a summary for the current type
5734                                                   verbose,                        // Verbose output?
5735                                                   depth + DEPTH_INCREMENT);       // Scope depth for any types that have children
5736                 }
5737 
5738                 // Indent the trailing squiggly bracket
5739                 if (element_idx > 0)
5740                     s->Printf("\n%*s}", depth, "");
5741             }
5742         }
5743         return;
5744 
5745     case clang::Type::Typedef:
5746         {
5747             QualType typedef_qual_type = cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType();
5748 
5749             ClangASTType typedef_clang_type (m_ast, typedef_qual_type);
5750             lldb::Format typedef_format = typedef_clang_type.GetFormat();
5751             std::pair<uint64_t, unsigned> typedef_type_info = m_ast->getTypeInfo(typedef_qual_type);
5752             uint64_t typedef_byte_size = typedef_type_info.first / 8;
5753 
5754             return typedef_clang_type.DumpValue (exe_ctx,
5755                                                  s,                  // Stream to dump to
5756                                                  typedef_format,     // The format with which to display the element
5757                                                  data,               // Data buffer containing all bytes for this type
5758                                                  data_byte_offset,   // Offset into "data" where to grab value from
5759                                                  typedef_byte_size,  // Size of this type in bytes
5760                                                  bitfield_bit_size,  // Bitfield bit size
5761                                                  bitfield_bit_offset,// Bitfield bit offset
5762                                                  show_types,         // Boolean indicating if we should show the variable types
5763                                                  show_summary,       // Boolean indicating if we should show a summary for the current type
5764                                                  verbose,            // Verbose output?
5765                                                  depth);             // Scope depth for any types that have children
5766         }
5767         break;
5768 
5769     case clang::Type::Elaborated:
5770         {
5771             QualType elaborated_qual_type = cast<ElaboratedType>(qual_type)->getNamedType();
5772             ClangASTType elaborated_clang_type (m_ast, elaborated_qual_type);
5773             lldb::Format elaborated_format = elaborated_clang_type.GetFormat();
5774             std::pair<uint64_t, unsigned> elaborated_type_info = m_ast->getTypeInfo(elaborated_qual_type);
5775             uint64_t elaborated_byte_size = elaborated_type_info.first / 8;
5776 
5777             return elaborated_clang_type.DumpValue (exe_ctx,
5778                                                     s,                  // Stream to dump to
5779                                                     elaborated_format,  // The format with which to display the element
5780                                                     data,               // Data buffer containing all bytes for this type
5781                                                     data_byte_offset,   // Offset into "data" where to grab value from
5782                                                     elaborated_byte_size,  // Size of this type in bytes
5783                                                     bitfield_bit_size,  // Bitfield bit size
5784                                                     bitfield_bit_offset,// Bitfield bit offset
5785                                                     show_types,         // Boolean indicating if we should show the variable types
5786                                                     show_summary,       // Boolean indicating if we should show a summary for the current type
5787                                                     verbose,            // Verbose output?
5788                                                     depth);             // Scope depth for any types that have children
5789         }
5790         break;
5791 
5792     case clang::Type::Paren:
5793         {
5794             QualType desugar_qual_type = cast<ParenType>(qual_type)->desugar();
5795             ClangASTType desugar_clang_type (m_ast, desugar_qual_type);
5796 
5797             lldb::Format desugar_format = desugar_clang_type.GetFormat();
5798             std::pair<uint64_t, unsigned> desugar_type_info = m_ast->getTypeInfo(desugar_qual_type);
5799             uint64_t desugar_byte_size = desugar_type_info.first / 8;
5800 
5801             return desugar_clang_type.DumpValue (exe_ctx,
5802                                                  s,                  // Stream to dump to
5803                                                  desugar_format,  // The format with which to display the element
5804                                                  data,               // Data buffer containing all bytes for this type
5805                                                  data_byte_offset,   // Offset into "data" where to grab value from
5806                                                  desugar_byte_size,  // Size of this type in bytes
5807                                                  bitfield_bit_size,  // Bitfield bit size
5808                                                  bitfield_bit_offset,// Bitfield bit offset
5809                                                  show_types,         // Boolean indicating if we should show the variable types
5810                                                  show_summary,       // Boolean indicating if we should show a summary for the current type
5811                                                  verbose,            // Verbose output?
5812                                                  depth);             // Scope depth for any types that have children
5813         }
5814         break;
5815 
5816     default:
5817         // We are down the a scalar type that we just need to display.
5818         data.Dump(s,
5819                   data_byte_offset,
5820                   format,
5821                   data_byte_size,
5822                   1,
5823                   UINT32_MAX,
5824                   LLDB_INVALID_ADDRESS,
5825                   bitfield_bit_size,
5826                   bitfield_bit_offset);
5827 
5828         if (show_summary)
5829             DumpSummary (exe_ctx, s, data, data_byte_offset, data_byte_size);
5830         break;
5831     }
5832 }
5833 
5834 
5835 
5836 
5837 bool
DumpTypeValue(Stream * s,lldb::Format format,const lldb_private::DataExtractor & data,lldb::offset_t byte_offset,size_t byte_size,uint32_t bitfield_bit_size,uint32_t bitfield_bit_offset,ExecutionContextScope * exe_scope)5838 ClangASTType::DumpTypeValue (Stream *s,
5839                              lldb::Format format,
5840                              const lldb_private::DataExtractor &data,
5841                              lldb::offset_t byte_offset,
5842                              size_t byte_size,
5843                              uint32_t bitfield_bit_size,
5844                              uint32_t bitfield_bit_offset,
5845                              ExecutionContextScope *exe_scope)
5846 {
5847     if (!IsValid())
5848         return false;
5849     if (IsAggregateType())
5850     {
5851         return false;
5852     }
5853     else
5854     {
5855         QualType qual_type(GetQualType());
5856 
5857         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
5858         switch (type_class)
5859         {
5860         case clang::Type::Typedef:
5861             {
5862                 QualType typedef_qual_type = cast<TypedefType>(qual_type)->getDecl()->getUnderlyingType();
5863                 ClangASTType typedef_clang_type (m_ast, typedef_qual_type);
5864                 if (format == eFormatDefault)
5865                     format = typedef_clang_type.GetFormat();
5866                 std::pair<uint64_t, unsigned> typedef_type_info = m_ast->getTypeInfo(typedef_qual_type);
5867                 uint64_t typedef_byte_size = typedef_type_info.first / 8;
5868 
5869                 return typedef_clang_type.DumpTypeValue (s,
5870                                                          format,                 // The format with which to display the element
5871                                                          data,                   // Data buffer containing all bytes for this type
5872                                                          byte_offset,            // Offset into "data" where to grab value from
5873                                                          typedef_byte_size,      // Size of this type in bytes
5874                                                          bitfield_bit_size,      // Size in bits of a bitfield value, if zero don't treat as a bitfield
5875                                                          bitfield_bit_offset,    // Offset in bits of a bitfield value if bitfield_bit_size != 0
5876                                                          exe_scope);
5877             }
5878             break;
5879 
5880         case clang::Type::Enum:
5881             // If our format is enum or default, show the enumeration value as
5882             // its enumeration string value, else just display it as requested.
5883             if ((format == eFormatEnum || format == eFormatDefault) && GetCompleteType ())
5884             {
5885                 const EnumType *enum_type = cast<EnumType>(qual_type.getTypePtr());
5886                 const EnumDecl *enum_decl = enum_type->getDecl();
5887                 assert(enum_decl);
5888                 EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
5889                 const bool is_signed = qual_type->isSignedIntegerOrEnumerationType();
5890                 lldb::offset_t offset = byte_offset;
5891                 if (is_signed)
5892                 {
5893                     const int64_t enum_svalue = data.GetMaxS64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
5894                     for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
5895                     {
5896                         if (enum_pos->getInitVal().getSExtValue() == enum_svalue)
5897                         {
5898                             s->PutCString (enum_pos->getNameAsString().c_str());
5899                             return true;
5900                         }
5901                     }
5902                     // If we have gotten here we didn't get find the enumerator in the
5903                     // enum decl, so just print the integer.
5904                     s->Printf("%" PRIi64, enum_svalue);
5905                 }
5906                 else
5907                 {
5908                     const uint64_t enum_uvalue = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
5909                     for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
5910                     {
5911                         if (enum_pos->getInitVal().getZExtValue() == enum_uvalue)
5912                         {
5913                             s->PutCString (enum_pos->getNameAsString().c_str());
5914                             return true;
5915                         }
5916                     }
5917                     // If we have gotten here we didn't get find the enumerator in the
5918                     // enum decl, so just print the integer.
5919                     s->Printf("%" PRIu64, enum_uvalue);
5920                 }
5921                 return true;
5922             }
5923             // format was not enum, just fall through and dump the value as requested....
5924 
5925         default:
5926             // We are down the a scalar type that we just need to display.
5927             {
5928                 uint32_t item_count = 1;
5929                 // A few formats, we might need to modify our size and count for depending
5930                 // on how we are trying to display the value...
5931                 switch (format)
5932                 {
5933                     default:
5934                     case eFormatBoolean:
5935                     case eFormatBinary:
5936                     case eFormatComplex:
5937                     case eFormatCString:         // NULL terminated C strings
5938                     case eFormatDecimal:
5939                     case eFormatEnum:
5940                     case eFormatHex:
5941                     case eFormatHexUppercase:
5942                     case eFormatFloat:
5943                     case eFormatOctal:
5944                     case eFormatOSType:
5945                     case eFormatUnsigned:
5946                     case eFormatPointer:
5947                     case eFormatVectorOfChar:
5948                     case eFormatVectorOfSInt8:
5949                     case eFormatVectorOfUInt8:
5950                     case eFormatVectorOfSInt16:
5951                     case eFormatVectorOfUInt16:
5952                     case eFormatVectorOfSInt32:
5953                     case eFormatVectorOfUInt32:
5954                     case eFormatVectorOfSInt64:
5955                     case eFormatVectorOfUInt64:
5956                     case eFormatVectorOfFloat32:
5957                     case eFormatVectorOfFloat64:
5958                     case eFormatVectorOfUInt128:
5959                         break;
5960 
5961                     case eFormatChar:
5962                     case eFormatCharPrintable:
5963                     case eFormatCharArray:
5964                     case eFormatBytes:
5965                     case eFormatBytesWithASCII:
5966                         item_count = byte_size;
5967                         byte_size = 1;
5968                         break;
5969 
5970                     case eFormatUnicode16:
5971                         item_count = byte_size / 2;
5972                         byte_size = 2;
5973                         break;
5974 
5975                     case eFormatUnicode32:
5976                         item_count = byte_size / 4;
5977                         byte_size = 4;
5978                         break;
5979                 }
5980                 return data.Dump (s,
5981                                   byte_offset,
5982                                   format,
5983                                   byte_size,
5984                                   item_count,
5985                                   UINT32_MAX,
5986                                   LLDB_INVALID_ADDRESS,
5987                                   bitfield_bit_size,
5988                                   bitfield_bit_offset,
5989                                   exe_scope);
5990             }
5991             break;
5992         }
5993     }
5994     return 0;
5995 }
5996 
5997 
5998 
5999 void
DumpSummary(ExecutionContext * exe_ctx,Stream * s,const lldb_private::DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size)6000 ClangASTType::DumpSummary (ExecutionContext *exe_ctx,
6001                            Stream *s,
6002                            const lldb_private::DataExtractor &data,
6003                            lldb::offset_t data_byte_offset,
6004                            size_t data_byte_size)
6005 {
6006     uint32_t length = 0;
6007     if (IsCStringType (length))
6008     {
6009         if (exe_ctx)
6010         {
6011             Process *process = exe_ctx->GetProcessPtr();
6012             if (process)
6013             {
6014                 lldb::offset_t offset = data_byte_offset;
6015                 lldb::addr_t pointer_addresss = data.GetMaxU64(&offset, data_byte_size);
6016                 std::vector<uint8_t> buf;
6017                 if (length > 0)
6018                     buf.resize (length);
6019                 else
6020                     buf.resize (256);
6021 
6022                 lldb_private::DataExtractor cstr_data(&buf.front(), buf.size(), process->GetByteOrder(), 4);
6023                 buf.back() = '\0';
6024                 size_t bytes_read;
6025                 size_t total_cstr_len = 0;
6026                 Error error;
6027                 while ((bytes_read = process->ReadMemory (pointer_addresss, &buf.front(), buf.size(), error)) > 0)
6028                 {
6029                     const size_t len = strlen((const char *)&buf.front());
6030                     if (len == 0)
6031                         break;
6032                     if (total_cstr_len == 0)
6033                         s->PutCString (" \"");
6034                     cstr_data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
6035                     total_cstr_len += len;
6036                     if (len < buf.size())
6037                         break;
6038                     pointer_addresss += total_cstr_len;
6039                 }
6040                 if (total_cstr_len > 0)
6041                     s->PutChar ('"');
6042             }
6043         }
6044     }
6045 }
6046 
6047 void
DumpTypeDescription() const6048 ClangASTType::DumpTypeDescription () const
6049 {
6050     StreamFile s (stdout, false);
6051     DumpTypeDescription (&s);
6052     ClangASTMetadata *metadata = ClangASTContext::GetMetadata (m_ast, m_type);
6053     if (metadata)
6054     {
6055         metadata->Dump (&s);
6056     }
6057 }
6058 
6059 void
DumpTypeDescription(Stream * s) const6060 ClangASTType::DumpTypeDescription (Stream *s) const
6061 {
6062     if (IsValid())
6063     {
6064         QualType qual_type(GetQualType());
6065 
6066         SmallVector<char, 1024> buf;
6067         raw_svector_ostream llvm_ostrm (buf);
6068 
6069         const clang::Type::TypeClass type_class = qual_type->getTypeClass();
6070         switch (type_class)
6071         {
6072         case clang::Type::ObjCObject:
6073         case clang::Type::ObjCInterface:
6074             {
6075                 GetCompleteType ();
6076 
6077                 const ObjCObjectType *objc_class_type = dyn_cast<ObjCObjectType>(qual_type.getTypePtr());
6078                 assert (objc_class_type);
6079                 if (objc_class_type)
6080                 {
6081                     ObjCInterfaceDecl *class_interface_decl = objc_class_type->getInterface();
6082                     if (class_interface_decl)
6083                     {
6084                         PrintingPolicy policy = m_ast->getPrintingPolicy();
6085                         class_interface_decl->print(llvm_ostrm, policy, s->GetIndentLevel());
6086                     }
6087                 }
6088             }
6089             break;
6090 
6091         case clang::Type::Typedef:
6092             {
6093                 const TypedefType *typedef_type = qual_type->getAs<TypedefType>();
6094                 if (typedef_type)
6095                 {
6096                     const TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6097                     std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
6098                     if (!clang_typedef_name.empty())
6099                     {
6100                         s->PutCString ("typedef ");
6101                         s->PutCString (clang_typedef_name.c_str());
6102                     }
6103                 }
6104             }
6105             break;
6106 
6107         case clang::Type::Elaborated:
6108             ClangASTType (m_ast, cast<ElaboratedType>(qual_type)->getNamedType()).DumpTypeDescription(s);
6109             return;
6110 
6111         case clang::Type::Paren:
6112             ClangASTType (m_ast, cast<ParenType>(qual_type)->desugar()).DumpTypeDescription(s);
6113             return;
6114 
6115         case clang::Type::Record:
6116             {
6117                 GetCompleteType ();
6118 
6119                 const RecordType *record_type = cast<RecordType>(qual_type.getTypePtr());
6120                 const RecordDecl *record_decl = record_type->getDecl();
6121                 const CXXRecordDecl *cxx_record_decl = dyn_cast<CXXRecordDecl>(record_decl);
6122 
6123                 if (cxx_record_decl)
6124                     cxx_record_decl->print(llvm_ostrm, m_ast->getPrintingPolicy(), s->GetIndentLevel());
6125                 else
6126                     record_decl->print(llvm_ostrm, m_ast->getPrintingPolicy(), s->GetIndentLevel());
6127             }
6128             break;
6129 
6130         default:
6131             {
6132                 const TagType *tag_type = dyn_cast<TagType>(qual_type.getTypePtr());
6133                 if (tag_type)
6134                 {
6135                     TagDecl *tag_decl = tag_type->getDecl();
6136                     if (tag_decl)
6137                         tag_decl->print(llvm_ostrm, 0);
6138                 }
6139                 else
6140                 {
6141                     std::string clang_type_name(qual_type.getAsString());
6142                     if (!clang_type_name.empty())
6143                         s->PutCString (clang_type_name.c_str());
6144                 }
6145             }
6146         }
6147 
6148         llvm_ostrm.flush();
6149         if (buf.size() > 0)
6150         {
6151             s->Write (buf.data(), buf.size());
6152         }
6153     }
6154 }
6155 
6156 bool
GetValueAsScalar(const lldb_private::DataExtractor & data,lldb::offset_t data_byte_offset,size_t data_byte_size,Scalar & value) const6157 ClangASTType::GetValueAsScalar (const lldb_private::DataExtractor &data,
6158                                 lldb::offset_t data_byte_offset,
6159                                 size_t data_byte_size,
6160                                 Scalar &value) const
6161 {
6162     if (!IsValid())
6163         return false;
6164 
6165     if (IsAggregateType ())
6166     {
6167         return false;   // Aggregate types don't have scalar values
6168     }
6169     else
6170     {
6171         uint64_t count = 0;
6172         lldb::Encoding encoding = GetEncoding (count);
6173 
6174         if (encoding == lldb::eEncodingInvalid || count != 1)
6175             return false;
6176 
6177         const uint64_t byte_size = GetByteSize();
6178         lldb::offset_t offset = data_byte_offset;
6179         switch (encoding)
6180         {
6181         case lldb::eEncodingInvalid:
6182             break;
6183         case lldb::eEncodingVector:
6184             break;
6185         case lldb::eEncodingUint:
6186             if (byte_size <= sizeof(unsigned long long))
6187             {
6188                 uint64_t uval64 = data.GetMaxU64 (&offset, byte_size);
6189                 if (byte_size <= sizeof(unsigned int))
6190                 {
6191                     value = (unsigned int)uval64;
6192                     return true;
6193                 }
6194                 else if (byte_size <= sizeof(unsigned long))
6195                 {
6196                     value = (unsigned long)uval64;
6197                     return true;
6198                 }
6199                 else if (byte_size <= sizeof(unsigned long long))
6200                 {
6201                     value = (unsigned long long )uval64;
6202                     return true;
6203                 }
6204                 else
6205                     value.Clear();
6206             }
6207             break;
6208 
6209         case lldb::eEncodingSint:
6210             if (byte_size <= sizeof(long long))
6211             {
6212                 int64_t sval64 = data.GetMaxS64 (&offset, byte_size);
6213                 if (byte_size <= sizeof(int))
6214                 {
6215                     value = (int)sval64;
6216                     return true;
6217                 }
6218                 else if (byte_size <= sizeof(long))
6219                 {
6220                     value = (long)sval64;
6221                     return true;
6222                 }
6223                 else if (byte_size <= sizeof(long long))
6224                 {
6225                     value = (long long )sval64;
6226                     return true;
6227                 }
6228                 else
6229                     value.Clear();
6230             }
6231             break;
6232 
6233         case lldb::eEncodingIEEE754:
6234             if (byte_size <= sizeof(long double))
6235             {
6236                 uint32_t u32;
6237                 uint64_t u64;
6238                 if (byte_size == sizeof(float))
6239                 {
6240                     if (sizeof(float) == sizeof(uint32_t))
6241                     {
6242                         u32 = data.GetU32(&offset);
6243                         value = *((float *)&u32);
6244                         return true;
6245                     }
6246                     else if (sizeof(float) == sizeof(uint64_t))
6247                     {
6248                         u64 = data.GetU64(&offset);
6249                         value = *((float *)&u64);
6250                         return true;
6251                     }
6252                 }
6253                 else
6254                 if (byte_size == sizeof(double))
6255                 {
6256                     if (sizeof(double) == sizeof(uint32_t))
6257                     {
6258                         u32 = data.GetU32(&offset);
6259                         value = *((double *)&u32);
6260                         return true;
6261                     }
6262                     else if (sizeof(double) == sizeof(uint64_t))
6263                     {
6264                         u64 = data.GetU64(&offset);
6265                         value = *((double *)&u64);
6266                         return true;
6267                     }
6268                 }
6269                 else
6270                 if (byte_size == sizeof(long double))
6271                 {
6272                     if (sizeof(long double) == sizeof(uint32_t))
6273                     {
6274                         u32 = data.GetU32(&offset);
6275                         value = *((long double *)&u32);
6276                         return true;
6277                     }
6278                     else if (sizeof(long double) == sizeof(uint64_t))
6279                     {
6280                         u64 = data.GetU64(&offset);
6281                         value = *((long double *)&u64);
6282                         return true;
6283                     }
6284                 }
6285             }
6286             break;
6287         }
6288     }
6289     return false;
6290 }
6291 
6292 bool
SetValueFromScalar(const Scalar & value,Stream & strm)6293 ClangASTType::SetValueFromScalar (const Scalar &value, Stream &strm)
6294 {
6295     // Aggregate types don't have scalar values
6296     if (!IsAggregateType ())
6297     {
6298         strm.GetFlags().Set(Stream::eBinary);
6299         uint64_t count = 0;
6300         lldb::Encoding encoding = GetEncoding (count);
6301 
6302         if (encoding == lldb::eEncodingInvalid || count != 1)
6303             return false;
6304 
6305         const uint64_t bit_width = GetBitSize();
6306         // This function doesn't currently handle non-byte aligned assignments
6307         if ((bit_width % 8) != 0)
6308             return false;
6309 
6310         const uint64_t byte_size = (bit_width + 7 ) / 8;
6311         switch (encoding)
6312         {
6313         case lldb::eEncodingInvalid:
6314             break;
6315         case lldb::eEncodingVector:
6316             break;
6317         case lldb::eEncodingUint:
6318             switch (byte_size)
6319             {
6320             case 1: strm.PutHex8(value.UInt()); return true;
6321             case 2: strm.PutHex16(value.UInt()); return true;
6322             case 4: strm.PutHex32(value.UInt()); return true;
6323             case 8: strm.PutHex64(value.ULongLong()); return true;
6324             default:
6325                 break;
6326             }
6327             break;
6328 
6329         case lldb::eEncodingSint:
6330             switch (byte_size)
6331             {
6332             case 1: strm.PutHex8(value.SInt()); return true;
6333             case 2: strm.PutHex16(value.SInt()); return true;
6334             case 4: strm.PutHex32(value.SInt()); return true;
6335             case 8: strm.PutHex64(value.SLongLong()); return true;
6336             default:
6337                 break;
6338             }
6339             break;
6340 
6341         case lldb::eEncodingIEEE754:
6342             if (byte_size <= sizeof(long double))
6343             {
6344                 if (byte_size == sizeof(float))
6345                 {
6346                     strm.PutFloat(value.Float());
6347                     return true;
6348                 }
6349                 else
6350                 if (byte_size == sizeof(double))
6351                 {
6352                     strm.PutDouble(value.Double());
6353                     return true;
6354                 }
6355                 else
6356                 if (byte_size == sizeof(long double))
6357                 {
6358                     strm.PutDouble(value.LongDouble());
6359                     return true;
6360                 }
6361             }
6362             break;
6363         }
6364     }
6365     return false;
6366 }
6367 
6368 bool
ReadFromMemory(lldb_private::ExecutionContext * exe_ctx,lldb::addr_t addr,AddressType address_type,lldb_private::DataExtractor & data)6369 ClangASTType::ReadFromMemory (lldb_private::ExecutionContext *exe_ctx,
6370                               lldb::addr_t addr,
6371                               AddressType address_type,
6372                               lldb_private::DataExtractor &data)
6373 {
6374     if (!IsValid())
6375         return false;
6376 
6377     // Can't convert a file address to anything valid without more
6378     // context (which Module it came from)
6379     if (address_type == eAddressTypeFile)
6380         return false;
6381 
6382     if (!GetCompleteType())
6383         return false;
6384 
6385     const uint64_t byte_size = GetByteSize();
6386     if (data.GetByteSize() < byte_size)
6387     {
6388         lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0'));
6389         data.SetData(data_sp);
6390     }
6391 
6392     uint8_t* dst = (uint8_t*)data.PeekData(0, byte_size);
6393     if (dst != NULL)
6394     {
6395         if (address_type == eAddressTypeHost)
6396         {
6397             if (addr == 0)
6398                 return false;
6399             // The address is an address in this process, so just copy it
6400             memcpy (dst, (uint8_t*)NULL + addr, byte_size);
6401             return true;
6402         }
6403         else
6404         {
6405             Process *process = NULL;
6406             if (exe_ctx)
6407                 process = exe_ctx->GetProcessPtr();
6408             if (process)
6409             {
6410                 Error error;
6411                 return process->ReadMemory(addr, dst, byte_size, error) == byte_size;
6412             }
6413         }
6414     }
6415     return false;
6416 }
6417 
6418 bool
WriteToMemory(lldb_private::ExecutionContext * exe_ctx,lldb::addr_t addr,AddressType address_type,StreamString & new_value)6419 ClangASTType::WriteToMemory (lldb_private::ExecutionContext *exe_ctx,
6420                              lldb::addr_t addr,
6421                              AddressType address_type,
6422                              StreamString &new_value)
6423 {
6424     if (!IsValid())
6425         return false;
6426 
6427     // Can't convert a file address to anything valid without more
6428     // context (which Module it came from)
6429     if (address_type == eAddressTypeFile)
6430         return false;
6431 
6432     if (!GetCompleteType())
6433         return false;
6434 
6435     const uint64_t byte_size = GetByteSize();
6436 
6437     if (byte_size > 0)
6438     {
6439         if (address_type == eAddressTypeHost)
6440         {
6441             // The address is an address in this process, so just copy it
6442             memcpy ((void *)addr, new_value.GetData(), byte_size);
6443             return true;
6444         }
6445         else
6446         {
6447             Process *process = NULL;
6448             if (exe_ctx)
6449                 process = exe_ctx->GetProcessPtr();
6450             if (process)
6451             {
6452                 Error error;
6453                 return process->WriteMemory(addr, new_value.GetData(), byte_size, error) == byte_size;
6454             }
6455         }
6456     }
6457     return false;
6458 }
6459 
6460 
6461 //CXXRecordDecl *
6462 //ClangASTType::GetAsCXXRecordDecl (lldb::clang_type_t opaque_clang_qual_type)
6463 //{
6464 //    if (opaque_clang_qual_type)
6465 //        return QualType::getFromOpaquePtr(opaque_clang_qual_type)->getAsCXXRecordDecl();
6466 //    return NULL;
6467 //}
6468 
6469 bool
operator ==(const lldb_private::ClangASTType & lhs,const lldb_private::ClangASTType & rhs)6470 lldb_private::operator == (const lldb_private::ClangASTType &lhs, const lldb_private::ClangASTType &rhs)
6471 {
6472     return lhs.GetASTContext() == rhs.GetASTContext() && lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType();
6473 }
6474 
6475 
6476 bool
operator !=(const lldb_private::ClangASTType & lhs,const lldb_private::ClangASTType & rhs)6477 lldb_private::operator != (const lldb_private::ClangASTType &lhs, const lldb_private::ClangASTType &rhs)
6478 {
6479     return lhs.GetASTContext() != rhs.GetASTContext() || lhs.GetOpaqueQualType() != rhs.GetOpaqueQualType();
6480 }
6481 
6482 
6483