1 //===-- Type.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 // Other libraries and framework includes
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/Decl.h"
14 #include "clang/AST/DeclCXX.h"
15 #include "clang/AST/DeclGroup.h"
16 #include "clang/AST/RecordLayout.h"
17 
18 #include "clang/Basic/Builtins.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Basic/LangOptions.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Basic/TargetInfo.h"
23 
24 #include "llvm/Support/FormattedStream.h"
25 #include "llvm/Support/raw_ostream.h"
26 
27 #include "lldb/Core/DataExtractor.h"
28 #include "lldb/Core/DataBufferHeap.h"
29 #include "lldb/Core/Module.h"
30 #include "lldb/Core/Scalar.h"
31 #include "lldb/Core/StreamString.h"
32 
33 #include "lldb/Symbol/ClangASTContext.h"
34 #include "lldb/Symbol/ObjectFile.h"
35 #include "lldb/Symbol/SymbolContextScope.h"
36 #include "lldb/Symbol/SymbolFile.h"
37 #include "lldb/Symbol/Type.h"
38 #include "lldb/Symbol/TypeList.h"
39 
40 #include "lldb/Target/ExecutionContext.h"
41 #include "lldb/Target/Process.h"
42 
43 lldb_private::Type::Type
44 (
45     lldb::user_id_t uid,
46     SymbolFile* symbol_file,
47     const ConstString &name,
48     uint64_t byte_size,
49     SymbolContextScope *context,
50     lldb::user_id_t encoding_uid,
51     EncodingUIDType encoding_uid_type,
52     const Declaration& decl,
53     void *clang_type
54 ) :
55     UserID (uid),
56     m_name (name),
57     m_byte_size (byte_size),
58     m_symbol_file (symbol_file),
59     m_context (context),
60     m_encoding_uid (encoding_uid),
61     m_encoding_uid_type (encoding_uid_type),
62     m_decl (decl),
63     m_clang_qual_type (clang_type)
64 {
65 }
66 
67 lldb_private::Type::Type () :
68     UserID (0),
69     m_name ("<INVALID TYPE>"),
70     m_byte_size (0),
71     m_symbol_file (NULL),
72     m_context (),
73     m_encoding_uid (0),
74     m_encoding_uid_type (eTypeInvalid),
75     m_decl (),
76     m_clang_qual_type (NULL)
77 {
78 }
79 
80 
81 const lldb_private::Type&
82 lldb_private::Type::operator= (const Type& rhs)
83 {
84     if (this != &rhs)
85     {
86         UserID::operator= (rhs);
87         m_name = rhs.m_name;
88         m_byte_size = rhs.m_byte_size;
89         m_symbol_file = rhs.m_symbol_file;
90         m_context = rhs.m_context;
91         m_encoding_uid = rhs.m_encoding_uid;
92         m_decl = rhs.m_decl;
93         m_clang_qual_type = rhs.m_clang_qual_type;
94     }
95     return *this;
96 }
97 
98 
99 void
100 lldb_private::Type::Dump (Stream *s, bool show_context)
101 {
102     s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
103     s->Indent();
104     *s << "Type" << (const UserID&)*this << ' ';
105     if (m_name)
106         *s << ", name = \"" << m_name << "\"";
107 
108     if (m_byte_size != 0)
109         s->Printf(", size = %zu", m_byte_size);
110 
111     if (show_context && m_context != NULL)
112     {
113         s->PutCString(", context = ( ");
114         m_context->DumpSymbolContext(s);
115         s->PutCString(" )");
116     }
117 
118     m_decl.Dump(s);
119 
120     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(m_clang_qual_type));
121 
122     if (qual_type.getTypePtr())
123     {
124         *s << ", clang_type = ";
125 
126         clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr());
127         clang::TagDecl *tag_decl = NULL;
128         if (tag_type)
129             tag_decl = tag_type->getDecl();
130 
131         if (tag_decl)
132         {
133             s->EOL();
134             s->EOL();
135             tag_decl->print(llvm::fouts(), 0);
136             s->EOL();
137         }
138         else
139         {
140             const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
141             if (typedef_type)
142             {
143                 const clang::TypedefDecl *typedef_decl = typedef_type->getDecl();
144                 std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
145                 if (!clang_typedef_name.empty())
146                     *s << " (" << clang_typedef_name.c_str() << ')';
147             }
148             else
149             {
150                 // We have a clang type, lets show it
151                 TypeList *type_list = GetTypeList();
152                 if (type_list)
153                 {
154                     clang::ASTContext *ast_context = GetClangAST();
155                     if (ast_context)
156                     {
157                         std::string clang_type_name(qual_type.getAsString());
158                         if (!clang_type_name.empty())
159                             *s << " (" << clang_type_name.c_str() << ')';
160                     }
161                 }
162             }
163         }
164     }
165     else if (m_encoding_uid != LLDB_INVALID_UID)
166     {
167         *s << ", type_uid = " << m_encoding_uid;
168         switch (m_encoding_uid_type)
169         {
170         case eIsTypeWithUID: s->PutCString(" (unresolved type)"); break;
171         case eIsConstTypeWithUID: s->PutCString(" (unresolved const type)"); break;
172         case eIsRestrictTypeWithUID: s->PutCString(" (unresolved restrict type)"); break;
173         case eIsVolatileTypeWithUID: s->PutCString(" (unresolved volatile type)"); break;
174         case eTypedefToTypeWithUID: s->PutCString(" (unresolved typedef)"); break;
175         case ePointerToTypeWithUID: s->PutCString(" (unresolved pointer)"); break;
176         case eLValueReferenceToTypeWithUID: s->PutCString(" (unresolved L value reference)"); break;
177         case eRValueReferenceToTypeWithUID: s->PutCString(" (unresolved R value reference)"); break;
178         }
179     }
180 
181 //
182 //  if (m_access)
183 //      s->Printf(", access = %u", m_access);
184     s->EOL();
185 }
186 
187 const lldb_private::ConstString &
188 lldb_private::Type::GetName()
189 {
190     if (!(m_name))
191     {
192         if (ResolveClangType())
193         {
194             std::string type_name = ClangASTContext::GetTypeName (m_clang_qual_type);
195             if (!type_name.empty())
196                 m_name.SetCString (type_name.c_str());
197         }
198     }
199     return m_name;
200 }
201 
202 int
203 lldb_private::Type::DumpClangTypeName(Stream *s, void *clang_type)
204 {
205     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
206     std::string type_name;
207     const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
208     if (typedef_type)
209     {
210         const clang::TypedefDecl *typedef_decl = typedef_type->getDecl();
211         type_name = typedef_decl->getQualifiedNameAsString();
212     }
213     else
214     {
215         type_name = qual_type.getAsString();
216     }
217     if (!type_name.empty())
218         return s->Printf("(%s) ", type_name.c_str());
219     return 0;
220 }
221 
222 lldb_private::ConstString
223 lldb_private::Type::GetClangTypeName (void *clang_type)
224 {
225     ConstString clang_type_name;
226     if (clang_type)
227     {
228         clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
229 
230         const clang::TypedefType *typedef_type = qual_type->getAs<clang::TypedefType>();
231         if (typedef_type)
232         {
233             const clang::TypedefDecl *typedef_decl = typedef_type->getDecl();
234             std::string clang_typedef_name (typedef_decl->getQualifiedNameAsString());
235             if (!clang_typedef_name.empty())
236                 clang_type_name.SetCString (clang_typedef_name.c_str());
237         }
238         else
239         {
240             std::string type_name(qual_type.getAsString());
241             if (!type_name.empty())
242                 clang_type_name.SetCString (type_name.c_str());
243         }
244     }
245     else
246     {
247         clang_type_name.SetCString ("<invalid>");
248     }
249 
250     return clang_type_name;
251 }
252 
253 
254 
255 void
256 lldb_private::Type::DumpTypeName(Stream *s)
257 {
258     GetName().Dump(s, "<invalid-type-name>");
259 }
260 
261 
262 void
263 lldb_private::Type::DumpValue
264 (
265     lldb_private::ExecutionContext *exe_ctx,
266     lldb_private::Stream *s,
267     const lldb_private::DataExtractor &data,
268     uint32_t data_byte_offset,
269     bool show_types,
270     bool show_summary,
271     bool verbose,
272     lldb::Format format
273 )
274 {
275     if (ResolveClangType())
276     {
277         if (show_types)
278         {
279             s->PutChar('(');
280             if (verbose)
281                 s->Printf("Type{0x%8.8x} ", GetID());
282             DumpTypeName (s);
283             s->PutCString(") ");
284         }
285 
286         lldb_private::Type::DumpValue (exe_ctx,
287                                        GetClangAST (),
288                                        m_clang_qual_type,
289                                        s,
290                                        format == lldb::eFormatDefault ? GetFormat() : format,
291                                        data,
292                                        data_byte_offset,
293                                        GetByteSize(),
294                                        0, // Bitfield bit size
295                                        0, // Bitfield bit offset
296                                        show_types,
297                                        show_summary,
298                                        verbose,
299                                        0);
300     }
301 }
302 
303 
304 void
305 lldb_private::Type::DumpSummary
306 (
307     ExecutionContext *exe_ctx,
308     clang::ASTContext *ast_context,
309     void *clang_type,
310     Stream *s,
311     const lldb_private::DataExtractor &data,
312     uint32_t data_byte_offset,
313     size_t data_byte_size
314 )
315 {
316     uint32_t length = 0;
317     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
318     if (ClangASTContext::IsCStringType (clang_type, length))
319     {
320 
321         if (exe_ctx && exe_ctx->process)
322         {
323             uint32_t offset = data_byte_offset;
324             lldb::addr_t pointer_addresss = data.GetMaxU64(&offset, data_byte_size);
325             const size_t k_max_buf_size = length ? length : 256;
326             uint8_t buf[k_max_buf_size + 1];
327             lldb_private::DataExtractor data(buf, k_max_buf_size, exe_ctx->process->GetByteOrder(), 4);
328             buf[k_max_buf_size] = '\0';
329             size_t bytes_read;
330             size_t total_cstr_len = 0;
331             Error error;
332             while ((bytes_read = exe_ctx->process->ReadMemory (pointer_addresss, buf, k_max_buf_size, error)) > 0)
333             {
334                 const size_t len = strlen((const char *)buf);
335                 if (len == 0)
336                     break;
337                 if (total_cstr_len == 0)
338                     s->PutCString (" \"");
339                 data.Dump(s, 0, lldb::eFormatChar, 1, len, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
340                 total_cstr_len += len;
341                 if (len < k_max_buf_size)
342                     break;
343                 pointer_addresss += total_cstr_len;
344             }
345             if (total_cstr_len > 0)
346                 s->PutChar ('"');
347         }
348     }
349 }
350 
351 #define DEPTH_INCREMENT 2
352 void
353 lldb_private::Type::DumpValue
354 (
355     ExecutionContext *exe_ctx,
356     clang::ASTContext *ast_context,
357     void *clang_type,
358     Stream *s,
359     lldb::Format format,
360     const lldb_private::DataExtractor &data,
361     uint32_t data_byte_offset,
362     size_t data_byte_size,
363     uint32_t bitfield_bit_size,
364     uint32_t bitfield_bit_offset,
365     bool show_types,
366     bool show_summary,
367     bool verbose,
368     uint32_t depth
369 )
370 {
371     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
372     switch (qual_type->getTypeClass())
373     {
374     case clang::Type::Record:
375         {
376             const clang::RecordType *record_type = cast<clang::RecordType>(qual_type.getTypePtr());
377             const clang::RecordDecl *record_decl = record_type->getDecl();
378             assert(record_decl);
379             uint32_t field_bit_offset = 0;
380             uint32_t field_byte_offset = 0;
381             const clang::ASTRecordLayout &record_layout = ast_context->getASTRecordLayout(record_decl);
382             uint32_t child_idx = 0;
383 
384 
385             const clang::CXXRecordDecl *cxx_record_decl = dyn_cast<clang::CXXRecordDecl>(record_decl);
386             if (cxx_record_decl)
387             {
388                 // We might have base classes to print out first
389                 clang::CXXRecordDecl::base_class_const_iterator base_class, base_class_end;
390                 for (base_class = cxx_record_decl->bases_begin(), base_class_end = cxx_record_decl->bases_end();
391                      base_class != base_class_end;
392                      ++base_class)
393                 {
394                     const clang::CXXRecordDecl *base_class_decl = cast<clang::CXXRecordDecl>(base_class->getType()->getAs<clang::RecordType>()->getDecl());
395 
396                     // Skip empty base classes
397                     if (verbose == false && ClangASTContext::RecordHasFields(base_class_decl) == false)
398                         continue;
399 
400                     if (base_class->isVirtual())
401                         field_bit_offset = record_layout.getVBaseClassOffset(base_class_decl);
402                     else
403                         field_bit_offset = record_layout.getBaseClassOffset(base_class_decl);
404                     field_byte_offset = field_bit_offset / 8;
405                     assert (field_bit_offset % 8 == 0);
406                     if (child_idx == 0)
407                         s->PutChar('{');
408                     else
409                         s->PutChar(',');
410 
411                     clang::QualType base_class_qual_type = base_class->getType();
412                     std::string base_class_type_name(base_class_qual_type.getAsString());
413 
414                     // Indent and print the base class type name
415                     s->Printf("\n%*s%s ", depth + DEPTH_INCREMENT, "", base_class_type_name.c_str());
416 
417                     std::pair<uint64_t, unsigned> base_class_type_info = ast_context->getTypeInfo(base_class_qual_type);
418 
419                     // Dump the value of the member
420                     Type::DumpValue (
421                         exe_ctx,
422                         ast_context,                        // The clang AST context for this type
423                         base_class_qual_type.getAsOpaquePtr(),// The clang type we want to dump
424                         s,                                  // Stream to dump to
425                         Type::GetFormat(base_class_qual_type.getAsOpaquePtr()), // The format with which to display the member
426                         data,                               // Data buffer containing all bytes for this type
427                         data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
428                         base_class_type_info.first / 8,     // Size of this type in bytes
429                         0,                                  // Bitfield bit size
430                         0,                                  // Bitfield bit offset
431                         show_types,                         // Boolean indicating if we should show the variable types
432                         show_summary,                       // Boolean indicating if we should show a summary for the current type
433                         verbose,                            // Verbose output?
434                         depth + DEPTH_INCREMENT);           // Scope depth for any types that have children
435 
436                     ++child_idx;
437                 }
438             }
439             const unsigned num_fields = record_layout.getFieldCount();
440 
441             uint32_t field_idx = 0;
442             clang::RecordDecl::field_iterator field, field_end;
443             for (field = record_decl->field_begin(), field_end = record_decl->field_end(); field != field_end; ++field, ++field_idx, ++child_idx)
444             {
445                 // Print the starting squiggly bracket (if this is the
446                 // first member) or comman (for member 2 and beyong) for
447                 // the struct/union/class member.
448                 if (child_idx == 0)
449                     s->PutChar('{');
450                 else
451                     s->PutChar(',');
452 
453                 // Indent
454                 s->Printf("\n%*s", depth + DEPTH_INCREMENT, "");
455 
456                 clang::QualType field_type = field->getType();
457                 // Print the member type if requested
458                 // Figure out the type byte size (field_type_info.first) and
459                 // alignment (field_type_info.second) from the AST context.
460                 std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(field_type);
461                 assert(field_idx < num_fields);
462                 // Figure out the field offset within the current struct/union/class type
463                 field_bit_offset = record_layout.getFieldOffset (field_idx);
464                 field_byte_offset = field_bit_offset / 8;
465                 uint32_t field_bitfield_bit_size = 0;
466                 uint32_t field_bitfield_bit_offset = 0;
467                 if (ClangASTContext::FieldIsBitfield (ast_context, *field, field_bitfield_bit_size))
468                     field_bitfield_bit_offset = field_bit_offset % 8;
469 
470                 if (show_types)
471                 {
472                     std::string field_type_name(field_type.getAsString());
473                     if (field_bitfield_bit_size > 0)
474                         s->Printf("(%s:%u) ", field_type_name.c_str(), field_bitfield_bit_size);
475                     else
476                         s->Printf("(%s) ", field_type_name.c_str());
477                 }
478                 // Print the member name and equal sign
479                 s->Printf("%s = ", field->getNameAsString().c_str());
480 
481 
482                 // Dump the value of the member
483                 Type::DumpValue (
484                     exe_ctx,
485                     ast_context,                    // The clang AST context for this type
486                     field_type.getAsOpaquePtr(),    // The clang type we want to dump
487                     s,                              // Stream to dump to
488                     Type::GetFormat(field_type.getAsOpaquePtr()),   // The format with which to display the member
489                     data,                           // Data buffer containing all bytes for this type
490                     data_byte_offset + field_byte_offset,// Offset into "data" where to grab value from
491                     field_type_info.first / 8,      // Size of this type in bytes
492                     field_bitfield_bit_size,        // Bitfield bit size
493                     field_bitfield_bit_offset,      // Bitfield bit offset
494                     show_types,                     // Boolean indicating if we should show the variable types
495                     show_summary,                   // Boolean indicating if we should show a summary for the current type
496                     verbose,                        // Verbose output?
497                     depth + DEPTH_INCREMENT);       // Scope depth for any types that have children
498             }
499 
500             // Indent the trailing squiggly bracket
501             if (child_idx > 0)
502                 s->Printf("\n%*s}", depth, "");
503         }
504         return;
505 
506     case clang::Type::Enum:
507         {
508             const clang::EnumType *enum_type = cast<clang::EnumType>(qual_type.getTypePtr());
509             const clang::EnumDecl *enum_decl = enum_type->getDecl();
510             assert(enum_decl);
511             clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
512             uint32_t offset = data_byte_offset;
513             const int64_t enum_value = data.GetMaxU64Bitfield(&offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset);
514             for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
515             {
516                 if (enum_pos->getInitVal() == enum_value)
517                 {
518                     s->Printf("%s", enum_pos->getNameAsCString());
519                     return;
520                 }
521             }
522             // If we have gotten here we didn't get find the enumerator in the
523             // enum decl, so just print the integer.
524             s->Printf("%lli", enum_value);
525         }
526         return;
527 
528     case clang::Type::ConstantArray:
529         {
530             const clang::ConstantArrayType *array = cast<clang::ConstantArrayType>(qual_type.getTypePtr());
531             bool is_array_of_characters = false;
532             clang::QualType element_qual_type = array->getElementType();
533 
534             clang::Type *canonical_type = element_qual_type->getCanonicalTypeInternal().getTypePtr();
535             if (canonical_type)
536                 is_array_of_characters = canonical_type->isCharType();
537 
538             const uint64_t element_count = array->getSize().getLimitedValue();
539 
540             std::pair<uint64_t, unsigned> field_type_info = ast_context->getTypeInfo(element_qual_type);
541 
542             uint32_t element_idx = 0;
543             uint32_t element_offset = 0;
544             uint64_t element_byte_size = field_type_info.first / 8;
545             uint32_t element_stride = element_byte_size;
546 
547             if (is_array_of_characters)
548             {
549                 s->PutChar('"');
550                 data.Dump(s, data_byte_offset, lldb::eFormatChar, element_byte_size, element_count, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
551                 s->PutChar('"');
552                 return;
553             }
554             else
555             {
556                 lldb::Format element_format = Type::GetFormat(element_qual_type.getAsOpaquePtr());
557 
558                 for (element_idx = 0; element_idx < element_count; ++element_idx)
559                 {
560                     // Print the starting squiggly bracket (if this is the
561                     // first member) or comman (for member 2 and beyong) for
562                     // the struct/union/class member.
563                     if (element_idx == 0)
564                         s->PutChar('{');
565                     else
566                         s->PutChar(',');
567 
568                     // Indent and print the index
569                     s->Printf("\n%*s[%u] ", depth + DEPTH_INCREMENT, "", element_idx);
570 
571                     // Figure out the field offset within the current struct/union/class type
572                     element_offset = element_idx * element_stride;
573 
574                     // Dump the value of the member
575                     Type::DumpValue (
576                         exe_ctx,
577                         ast_context,                    // The clang AST context for this type
578                         element_qual_type.getAsOpaquePtr(), // The clang type we want to dump
579                         s,                              // Stream to dump to
580                         element_format,                 // The format with which to display the element
581                         data,                           // Data buffer containing all bytes for this type
582                         data_byte_offset + element_offset,// Offset into "data" where to grab value from
583                         element_byte_size,              // Size of this type in bytes
584                         0,                              // Bitfield bit size
585                         0,                              // Bitfield bit offset
586                         show_types,                     // Boolean indicating if we should show the variable types
587                         show_summary,                   // Boolean indicating if we should show a summary for the current type
588                         verbose,                        // Verbose output?
589                         depth + DEPTH_INCREMENT);       // Scope depth for any types that have children
590                 }
591 
592                 // Indent the trailing squiggly bracket
593                 if (element_idx > 0)
594                     s->Printf("\n%*s}", depth, "");
595             }
596         }
597         return;
598 
599     case clang::Type::Typedef:
600         {
601             clang::QualType typedef_qual_type = cast<clang::TypedefType>(qual_type)->LookThroughTypedefs();
602             lldb::Format typedef_format = lldb_private::Type::GetFormat(typedef_qual_type.getAsOpaquePtr());
603             std::pair<uint64_t, unsigned> typedef_type_info = ast_context->getTypeInfo(typedef_qual_type);
604             uint64_t typedef_byte_size = typedef_type_info.first / 8;
605 
606             return Type::DumpValue(
607                         exe_ctx,
608                         ast_context,        // The clang AST context for this type
609                         typedef_qual_type.getAsOpaquePtr(), // The clang type we want to dump
610                         s,                  // Stream to dump to
611                         typedef_format,     // The format with which to display the element
612                         data,               // Data buffer containing all bytes for this type
613                         data_byte_offset,   // Offset into "data" where to grab value from
614                         typedef_byte_size,  // Size of this type in bytes
615                         bitfield_bit_size,  // Bitfield bit size
616                         bitfield_bit_offset,// Bitfield bit offset
617                         show_types,         // Boolean indicating if we should show the variable types
618                         show_summary,       // Boolean indicating if we should show a summary for the current type
619                         verbose,            // Verbose output?
620                         depth);             // Scope depth for any types that have children
621         }
622         break;
623 
624     default:
625         // We are down the a scalar type that we just need to display.
626         data.Dump(s, data_byte_offset, format, data_byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, bitfield_bit_size, bitfield_bit_offset);
627 
628         if (show_summary)
629             Type::DumpSummary (exe_ctx, ast_context, clang_type, s, data, data_byte_offset, data_byte_size);
630         break;
631     }
632 }
633 
634 bool
635 lldb_private::Type::DumpTypeValue
636 (
637     Stream *s,
638     clang::ASTContext *ast_context,
639     void *clang_type,
640     lldb::Format format,
641     const lldb_private::DataExtractor &data,
642     uint32_t byte_offset,
643     size_t byte_size,
644     uint32_t bitfield_bit_size,
645     uint32_t bitfield_bit_offset
646 )
647 {
648     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
649     if (ClangASTContext::IsAggregateType (clang_type))
650     {
651         return 0;
652     }
653     else
654     {
655         switch (qual_type->getTypeClass())
656         {
657         case clang::Type::Enum:
658             {
659                 const clang::EnumType *enum_type = cast<clang::EnumType>(qual_type.getTypePtr());
660                 const clang::EnumDecl *enum_decl = enum_type->getDecl();
661                 assert(enum_decl);
662                 clang::EnumDecl::enumerator_iterator enum_pos, enum_end_pos;
663                 uint32_t offset = byte_offset;
664                 const int64_t enum_value = data.GetMaxU64Bitfield (&offset, byte_size, bitfield_bit_size, bitfield_bit_offset);
665                 for (enum_pos = enum_decl->enumerator_begin(), enum_end_pos = enum_decl->enumerator_end(); enum_pos != enum_end_pos; ++enum_pos)
666                 {
667                     if (enum_pos->getInitVal() == enum_value)
668                     {
669                         s->PutCString (enum_pos->getNameAsCString());
670                         return true;
671                     }
672                 }
673                 // If we have gotten here we didn't get find the enumerator in the
674                 // enum decl, so just print the integer.
675 
676                 s->Printf("%lli", enum_value);
677                 return true;
678             }
679             break;
680 
681         case clang::Type::Typedef:
682             {
683                 clang::QualType typedef_qual_type = cast<clang::TypedefType>(qual_type)->LookThroughTypedefs();
684                 lldb::Format typedef_format = Type::GetFormat(typedef_qual_type.getAsOpaquePtr());
685                 std::pair<uint64_t, unsigned> typedef_type_info = ast_context->getTypeInfo(typedef_qual_type);
686                 uint64_t typedef_byte_size = typedef_type_info.first / 8;
687 
688                 return Type::DumpTypeValue(
689                             s,
690                             ast_context,            // The clang AST context for this type
691                             typedef_qual_type.getAsOpaquePtr(),     // The clang type we want to dump
692                             typedef_format,         // The format with which to display the element
693                             data,                   // Data buffer containing all bytes for this type
694                             byte_offset,            // Offset into "data" where to grab value from
695                             typedef_byte_size,      // Size of this type in bytes
696                             bitfield_bit_size,      // Size in bits of a bitfield value, if zero don't treat as a bitfield
697                             bitfield_bit_offset);   // Offset in bits of a bitfield value if bitfield_bit_size != 0
698             }
699             break;
700 
701         default:
702             // We are down the a scalar type that we just need to display.
703             return data.Dump(s,
704                              byte_offset,
705                              format,
706                              byte_size,
707                              1,
708                              UINT32_MAX,
709                              LLDB_INVALID_ADDRESS,
710                              bitfield_bit_size,
711                              bitfield_bit_offset);
712             break;
713         }
714     }
715     return 0;
716 }
717 
718 bool
719 lldb_private::Type::GetValueAsScalar
720 (
721     clang::ASTContext *ast_context,
722     void *clang_type,
723     const lldb_private::DataExtractor &data,
724     uint32_t data_byte_offset,
725     size_t data_byte_size,
726     Scalar &value
727 )
728 {
729     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
730 
731     if (ClangASTContext::IsAggregateType (clang_type))
732     {
733         return false;   // Aggregate types don't have scalar values
734     }
735     else
736     {
737         uint32_t count = 0;
738         lldb::Encoding encoding = Type::GetEncoding (clang_type, count);
739 
740         if (encoding == lldb::eEncodingInvalid || count != 1)
741             return false;
742 
743         uint64_t bit_width = ast_context->getTypeSize(qual_type);
744         uint32_t byte_size = (bit_width + 7 ) / 8;
745         uint32_t offset = data_byte_offset;
746         switch (encoding)
747         {
748         case lldb::eEncodingUint:
749             if (byte_size <= sizeof(unsigned long long))
750             {
751                 uint64_t uval64 = data.GetMaxU64 (&offset, byte_size);
752                 if (byte_size <= sizeof(unsigned int))
753                 {
754                     value = (unsigned int)uval64;
755                     return true;
756                 }
757                 else if (byte_size <= sizeof(unsigned long))
758                 {
759                     value = (unsigned long)uval64;
760                     return true;
761                 }
762                 else if (byte_size <= sizeof(unsigned long long))
763                 {
764                     value = (unsigned long long )uval64;
765                     return true;
766                 }
767                 else
768                     value.Clear();
769             }
770             break;
771 
772         case lldb::eEncodingSint:
773             if (byte_size <= sizeof(long long))
774             {
775                 int64_t sval64 = (int64_t)data.GetMaxU64 (&offset, byte_size);
776                 if (byte_size <= sizeof(int))
777                 {
778                     value = (int)sval64;
779                     return true;
780                 }
781                 else if (byte_size <= sizeof(long))
782                 {
783                     value = (long)sval64;
784                     return true;
785                 }
786                 else if (byte_size <= sizeof(long long))
787                 {
788                     value = (long long )sval64;
789                     return true;
790                 }
791                 else
792                     value.Clear();
793             }
794             break;
795 
796         case lldb::eEncodingIEEE754:
797             if (byte_size <= sizeof(long double))
798             {
799                 uint32_t u32;
800                 uint64_t u64;
801                 if (byte_size == sizeof(float))
802                 {
803                     if (sizeof(float) == sizeof(uint32_t))
804                     {
805                         u32 = data.GetU32(&offset);
806                         value = *((float *)&u32);
807                         return true;
808                     }
809                     else if (sizeof(float) == sizeof(uint64_t))
810                     {
811                         u64 = data.GetU64(&offset);
812                         value = *((float *)&u64);
813                         return true;
814                     }
815                 }
816                 else
817                 if (byte_size == sizeof(double))
818                 {
819                     if (sizeof(double) == sizeof(uint32_t))
820                     {
821                         u32 = data.GetU32(&offset);
822                         value = *((double *)&u32);
823                         return true;
824                     }
825                     else if (sizeof(double) == sizeof(uint64_t))
826                     {
827                         u64 = data.GetU64(&offset);
828                         value = *((double *)&u64);
829                         return true;
830                     }
831                 }
832                 else
833                 if (byte_size == sizeof(long double))
834                 {
835                     if (sizeof(long double) == sizeof(uint32_t))
836                     {
837                         u32 = data.GetU32(&offset);
838                         value = *((long double *)&u32);
839                         return true;
840                     }
841                     else if (sizeof(long double) == sizeof(uint64_t))
842                     {
843                         u64 = data.GetU64(&offset);
844                         value = *((long double *)&u64);
845                         return true;
846                     }
847                 }
848             }
849             break;
850         }
851     }
852     return false;
853 }
854 
855 bool
856 lldb_private::Type::SetValueFromScalar
857 (
858     clang::ASTContext *ast_context,
859     void *clang_type,
860     const Scalar &value,
861     Stream &strm
862 )
863 {
864     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
865 
866     // Aggregate types don't have scalar values
867     if (!ClangASTContext::IsAggregateType (clang_type))
868     {
869         strm.GetFlags().Set(Stream::eBinary);
870         uint32_t count = 0;
871         lldb::Encoding encoding = Type::GetEncoding (clang_type, count);
872 
873         if (encoding == lldb::eEncodingInvalid || count != 1)
874             return false;
875 
876         uint64_t bit_width = ast_context->getTypeSize(qual_type);
877         // This function doesn't currently handle non-byte aligned assignments
878         if ((bit_width % 8) != 0)
879             return false;
880 
881         uint32_t byte_size = (bit_width + 7 ) / 8;
882         switch (encoding)
883         {
884         case lldb::eEncodingUint:
885             switch (byte_size)
886             {
887             case 1: strm.PutHex8(value.UInt()); return true;
888             case 2: strm.PutHex16(value.UInt()); return true;
889             case 4: strm.PutHex32(value.UInt()); return true;
890             case 8: strm.PutHex64(value.ULongLong()); return true;
891             default:
892                 break;
893             }
894             break;
895 
896         case lldb::eEncodingSint:
897             switch (byte_size)
898             {
899             case 1: strm.PutHex8(value.SInt()); return true;
900             case 2: strm.PutHex16(value.SInt()); return true;
901             case 4: strm.PutHex32(value.SInt()); return true;
902             case 8: strm.PutHex64(value.SLongLong()); return true;
903             default:
904                 break;
905             }
906             break;
907 
908         case lldb::eEncodingIEEE754:
909             if (byte_size <= sizeof(long double))
910             {
911                 if (byte_size == sizeof(float))
912                 {
913                     strm.PutFloat(value.Float());
914                     return true;
915                 }
916                 else
917                 if (byte_size == sizeof(double))
918                 {
919                     strm.PutDouble(value.Double());
920                     return true;
921                 }
922                 else
923                 if (byte_size == sizeof(long double))
924                 {
925                     strm.PutDouble(value.LongDouble());
926                     return true;
927                 }
928             }
929             break;
930         }
931     }
932     return false;
933 }
934 
935 
936 uint64_t
937 lldb_private::Type::GetByteSize()
938 {
939     if (m_byte_size == 0)
940     {
941         switch (m_encoding_uid_type)
942         {
943         case eIsTypeWithUID:
944         case eIsConstTypeWithUID:
945         case eIsRestrictTypeWithUID:
946         case eIsVolatileTypeWithUID:
947         case eTypedefToTypeWithUID:
948             if (m_encoding_uid != LLDB_INVALID_UID)
949             {
950                 Type *encoding_type = m_symbol_file->ResolveTypeUID (m_encoding_uid);
951                 if (encoding_type)
952                     m_byte_size = encoding_type->GetByteSize();
953             }
954             if (m_byte_size == 0)
955             {
956                 uint64_t bit_width = GetClangAST()->getTypeSize(clang::QualType::getFromOpaquePtr(GetOpaqueClangQualType()));
957                 m_byte_size = (bit_width + 7 ) / 8;
958             }
959             break;
960 
961         // If we are a pointer or reference, then this is just a pointer size;
962         case ePointerToTypeWithUID:
963         case eLValueReferenceToTypeWithUID:
964         case eRValueReferenceToTypeWithUID:
965             m_byte_size = GetTypeList()->GetClangASTContext().GetPointerBitSize() / 8;
966             break;
967         }
968     }
969     return m_byte_size;
970 }
971 
972 
973 uint32_t
974 lldb_private::Type::GetNumChildren (bool omit_empty_base_classes)
975 {
976     if (!ResolveClangType())
977         return 0;
978     return ClangASTContext::GetNumChildren (m_clang_qual_type, omit_empty_base_classes);
979 
980 }
981 
982 bool
983 lldb_private::Type::IsAggregateType ()
984 {
985     if (ResolveClangType())
986         return ClangASTContext::IsAggregateType (m_clang_qual_type);
987     return false;
988 }
989 
990 lldb::Format
991 lldb_private::Type::GetFormat ()
992 {
993     // Make sure we resolve our type if it already hasn't been.
994     if (!ResolveClangType())
995         return lldb::eFormatInvalid;
996     return lldb_private::Type::GetFormat (m_clang_qual_type);
997 }
998 
999 
1000 lldb::Format
1001 lldb_private::Type::GetFormat (void *clang_type)
1002 {
1003     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
1004 
1005     switch (qual_type->getTypeClass())
1006     {
1007     case clang::Type::FunctionNoProto:
1008     case clang::Type::FunctionProto:
1009         break;
1010 
1011     case clang::Type::IncompleteArray:
1012     case clang::Type::VariableArray:
1013         break;
1014 
1015     case clang::Type::ConstantArray:
1016         break;
1017 
1018     case clang::Type::ExtVector:
1019     case clang::Type::Vector:
1020         break;
1021 
1022     case clang::Type::Builtin:
1023         switch (cast<clang::BuiltinType>(qual_type)->getKind())
1024         {
1025         default: assert(0 && "Unknown builtin type!");
1026         case clang::BuiltinType::Void:
1027             break;
1028 
1029         case clang::BuiltinType::Bool:          return lldb::eFormatBoolean;
1030         case clang::BuiltinType::Char_S:
1031         case clang::BuiltinType::SChar:
1032         case clang::BuiltinType::Char_U:
1033         case clang::BuiltinType::UChar:
1034         case clang::BuiltinType::WChar:         return lldb::eFormatChar;
1035         case clang::BuiltinType::Char16:        return lldb::eFormatUnicode16;
1036         case clang::BuiltinType::Char32:        return lldb::eFormatUnicode32;
1037         case clang::BuiltinType::UShort:        return lldb::eFormatHex;
1038         case clang::BuiltinType::Short:         return lldb::eFormatDecimal;
1039         case clang::BuiltinType::UInt:          return lldb::eFormatHex;
1040         case clang::BuiltinType::Int:           return lldb::eFormatDecimal;
1041         case clang::BuiltinType::ULong:         return lldb::eFormatHex;
1042         case clang::BuiltinType::Long:          return lldb::eFormatDecimal;
1043         case clang::BuiltinType::ULongLong:     return lldb::eFormatHex;
1044         case clang::BuiltinType::LongLong:      return lldb::eFormatDecimal;
1045         case clang::BuiltinType::UInt128:       return lldb::eFormatHex;
1046         case clang::BuiltinType::Int128:        return lldb::eFormatDecimal;
1047         case clang::BuiltinType::Float:         return lldb::eFormatFloat;
1048         case clang::BuiltinType::Double:        return lldb::eFormatFloat;
1049         case clang::BuiltinType::LongDouble:    return lldb::eFormatFloat;
1050         case clang::BuiltinType::NullPtr:       return lldb::eFormatHex;
1051         }
1052         break;
1053     case clang::Type::ObjCObjectPointer:        return lldb::eFormatHex;
1054     case clang::Type::BlockPointer:             return lldb::eFormatHex;
1055     case clang::Type::Pointer:                  return lldb::eFormatHex;
1056     case clang::Type::LValueReference:
1057     case clang::Type::RValueReference:          return lldb::eFormatHex;
1058     case clang::Type::MemberPointer:            break;
1059     case clang::Type::Complex:                  return lldb::eFormatComplex;
1060     case clang::Type::ObjCInterface:            break;
1061     case clang::Type::Record:                   break;
1062     case clang::Type::Enum:                     return lldb::eFormatEnum;
1063     case clang::Type::Typedef:
1064         return lldb_private::Type::GetFormat(cast<clang::TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr());
1065 
1066     case clang::Type::TypeOfExpr:
1067     case clang::Type::TypeOf:
1068     case clang::Type::Decltype:
1069 //    case clang::Type::QualifiedName:
1070     case clang::Type::TemplateSpecialization:   break;
1071     }
1072     // We don't know hot to display this type...
1073     return lldb::eFormatBytes;
1074 }
1075 
1076 
1077 lldb::Encoding
1078 lldb_private::Type::GetEncoding (uint32_t &count)
1079 {
1080     // Make sure we resolve our type if it already hasn't been.
1081     if (!ResolveClangType())
1082         return lldb::eEncodingInvalid;
1083 
1084     return Type::GetEncoding (m_clang_qual_type, count);
1085 }
1086 
1087 
1088 lldb::Encoding
1089 lldb_private::Type::GetEncoding (void *clang_type, uint32_t &count)
1090 {
1091     count = 1;
1092     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
1093 
1094     switch (qual_type->getTypeClass())
1095     {
1096     case clang::Type::FunctionNoProto:
1097     case clang::Type::FunctionProto:
1098         break;
1099 
1100     case clang::Type::IncompleteArray:
1101     case clang::Type::VariableArray:
1102         break;
1103 
1104     case clang::Type::ConstantArray:
1105         break;
1106 
1107     case clang::Type::ExtVector:
1108     case clang::Type::Vector:
1109         // TODO: Set this to more than one???
1110         break;
1111 
1112     case clang::Type::Builtin:
1113         switch (cast<clang::BuiltinType>(qual_type)->getKind())
1114         {
1115         default: assert(0 && "Unknown builtin type!");
1116         case clang::BuiltinType::Void:
1117             break;
1118 
1119         case clang::BuiltinType::Bool:
1120         case clang::BuiltinType::Char_S:
1121         case clang::BuiltinType::SChar:
1122         case clang::BuiltinType::WChar:
1123         case clang::BuiltinType::Char16:
1124         case clang::BuiltinType::Char32:
1125         case clang::BuiltinType::Short:
1126         case clang::BuiltinType::Int:
1127         case clang::BuiltinType::Long:
1128         case clang::BuiltinType::LongLong:
1129         case clang::BuiltinType::Int128:        return lldb::eEncodingSint;
1130 
1131         case clang::BuiltinType::Char_U:
1132         case clang::BuiltinType::UChar:
1133         case clang::BuiltinType::UShort:
1134         case clang::BuiltinType::UInt:
1135         case clang::BuiltinType::ULong:
1136         case clang::BuiltinType::ULongLong:
1137         case clang::BuiltinType::UInt128:       return lldb::eEncodingUint;
1138 
1139         case clang::BuiltinType::Float:
1140         case clang::BuiltinType::Double:
1141         case clang::BuiltinType::LongDouble:    return lldb::eEncodingIEEE754;
1142 
1143         case clang::BuiltinType::NullPtr:       return lldb::eEncodingUint;
1144         }
1145         break;
1146     // All pointer types are represented as unsigned integer encodings.
1147     // We may nee to add a eEncodingPointer if we ever need to know the
1148     // difference
1149     case clang::Type::ObjCObjectPointer:
1150     case clang::Type::BlockPointer:
1151     case clang::Type::Pointer:
1152     case clang::Type::LValueReference:
1153     case clang::Type::RValueReference:
1154     case clang::Type::MemberPointer:            return lldb::eEncodingUint;
1155     // Complex numbers are made up of floats
1156     case clang::Type::Complex:
1157         count = 2;
1158         return lldb::eEncodingIEEE754;
1159 
1160     case clang::Type::ObjCInterface:            break;
1161     case clang::Type::Record:                   break;
1162     case clang::Type::Enum:                     return lldb::eEncodingSint;
1163     case clang::Type::Typedef:
1164         return Type::GetEncoding(cast<clang::TypedefType>(qual_type)->LookThroughTypedefs().getAsOpaquePtr(), count);
1165         break;
1166 
1167     case clang::Type::TypeOfExpr:
1168     case clang::Type::TypeOf:
1169     case clang::Type::Decltype:
1170 //    case clang::Type::QualifiedName:
1171     case clang::Type::TemplateSpecialization:   break;
1172     }
1173     count = 0;
1174     return lldb::eEncodingInvalid;
1175 }
1176 
1177 
1178 bool
1179 lldb_private::Type::DumpValueInMemory
1180 (
1181     lldb_private::ExecutionContext *exe_ctx,
1182     lldb_private::Stream *s,
1183     lldb::addr_t address,
1184     lldb::AddressType address_type,
1185     bool show_types,
1186     bool show_summary,
1187     bool verbose
1188 )
1189 {
1190     if (address != LLDB_INVALID_ADDRESS)
1191     {
1192         lldb_private::DataExtractor data;
1193         data.SetByteOrder (exe_ctx->process->GetByteOrder());
1194         if (ReadFromMemory (exe_ctx, address, address_type, data))
1195         {
1196             DumpValue(exe_ctx, s, data, 0, show_types, show_summary, verbose);
1197             return true;
1198         }
1199     }
1200     return false;
1201 }
1202 
1203 bool
1204 lldb_private::Type::ReadFromMemory
1205 (
1206     lldb_private::ExecutionContext *exe_ctx,
1207     clang::ASTContext *ast_context,
1208     void *clang_type,
1209     lldb::addr_t addr,
1210     lldb::AddressType address_type,
1211     lldb_private::DataExtractor &data
1212 )
1213 {
1214     if (address_type == lldb::eAddressTypeFile)
1215     {
1216         // Can't convert a file address to anything valid without more
1217         // context (which Module it came from)
1218         return false;
1219     }
1220     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
1221 
1222     const uint32_t byte_size = (ast_context->getTypeSize (qual_type) + 7) / 8;
1223     if (data.GetByteSize() < byte_size)
1224     {
1225         lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0'));
1226         data.SetData(data_sp);
1227     }
1228 
1229     uint8_t* dst = (uint8_t*)data.PeekData(0, byte_size);
1230     if (dst != NULL)
1231     {
1232         if (address_type == lldb::eAddressTypeHost)
1233         {
1234             // The address is an address in this process, so just copy it
1235             memcpy (dst, (uint8_t*)NULL + addr, byte_size);
1236             return true;
1237         }
1238         else
1239         {
1240             if (exe_ctx && exe_ctx->process)
1241             {
1242                 Error error;
1243                 return exe_ctx->process->ReadMemory(addr, dst, byte_size, error) == byte_size;
1244             }
1245         }
1246     }
1247     return false;
1248 }
1249 
1250 bool
1251 lldb_private::Type::WriteToMemory
1252 (
1253     lldb_private::ExecutionContext *exe_ctx,
1254     clang::ASTContext *ast_context,
1255     void *clang_type,
1256     lldb::addr_t addr,
1257     lldb::AddressType address_type,
1258     StreamString &new_value
1259 )
1260 {
1261     if (address_type == lldb::eAddressTypeFile)
1262     {
1263         // Can't convert a file address to anything valid without more
1264         // context (which Module it came from)
1265         return false;
1266     }
1267     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
1268     const uint32_t byte_size = (ast_context->getTypeSize (qual_type) + 7) / 8;
1269 
1270     if (byte_size > 0)
1271     {
1272         if (address_type == lldb::eAddressTypeHost)
1273         {
1274             // The address is an address in this process, so just copy it
1275             memcpy ((void *)addr, new_value.GetData(), byte_size);
1276             return true;
1277         }
1278         else
1279         {
1280             if (exe_ctx && exe_ctx->process)
1281             {
1282                 Error error;
1283                 return exe_ctx->process->WriteMemory(addr, new_value.GetData(), byte_size, error) == byte_size;
1284             }
1285         }
1286     }
1287     return false;
1288 }
1289 
1290 
1291 bool
1292 lldb_private::Type::ReadFromMemory (lldb_private::ExecutionContext *exe_ctx, lldb::addr_t addr, lldb::AddressType address_type, lldb_private::DataExtractor &data)
1293 {
1294     if (address_type == lldb::eAddressTypeFile)
1295     {
1296         // Can't convert a file address to anything valid without more
1297         // context (which Module it came from)
1298         return false;
1299     }
1300 
1301     const uint32_t byte_size = GetByteSize();
1302     if (data.GetByteSize() < byte_size)
1303     {
1304         lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0'));
1305         data.SetData(data_sp);
1306     }
1307 
1308     uint8_t* dst = (uint8_t*)data.PeekData(0, byte_size);
1309     if (dst != NULL)
1310     {
1311         if (address_type == lldb::eAddressTypeHost)
1312         {
1313             // The address is an address in this process, so just copy it
1314             memcpy (dst, (uint8_t*)NULL + addr, byte_size);
1315             return true;
1316         }
1317         else
1318         {
1319             if (exe_ctx && exe_ctx->process)
1320             {
1321                 Error error;
1322                 return exe_ctx->process->ReadMemory(addr, dst, byte_size, error) == byte_size;
1323             }
1324         }
1325     }
1326     return false;
1327 }
1328 
1329 
1330 bool
1331 lldb_private::Type::WriteToMemory (lldb_private::ExecutionContext *exe_ctx, lldb::addr_t addr, lldb::AddressType address_type, lldb_private::DataExtractor &data)
1332 {
1333     return false;
1334 }
1335 
1336 
1337 lldb_private::TypeList*
1338 lldb_private::Type::GetTypeList()
1339 {
1340     return GetSymbolFile()->GetObjectFile()->GetModule()->GetTypeList();
1341 }
1342 
1343 
1344 bool
1345 lldb_private::Type::ResolveClangType()
1346 {
1347     clang::QualType qual_type(clang::QualType::getFromOpaquePtr(m_clang_qual_type));
1348     if (qual_type.getTypePtr() == NULL)
1349     {
1350         clang::QualType resolved_qual_type;
1351         TypeList *type_list = GetTypeList();
1352         if (m_encoding_uid != LLDB_INVALID_UID)
1353         {
1354             Type *encoding_type = m_symbol_file->ResolveTypeUID(m_encoding_uid);
1355             if (encoding_type)
1356             {
1357 
1358                 switch (m_encoding_uid_type)
1359                 {
1360                 case eIsTypeWithUID:
1361                     resolved_qual_type = clang::QualType::getFromOpaquePtr(encoding_type->GetOpaqueClangQualType());
1362                     break;
1363 
1364                 case eIsConstTypeWithUID:
1365                     resolved_qual_type = clang::QualType::getFromOpaquePtr(ClangASTContext::AddConstModifier (encoding_type->GetOpaqueClangQualType()));
1366                     break;
1367 
1368                 case eIsRestrictTypeWithUID:
1369                     resolved_qual_type = clang::QualType::getFromOpaquePtr(ClangASTContext::AddRestrictModifier (encoding_type->GetOpaqueClangQualType()));
1370                     break;
1371 
1372                 case eIsVolatileTypeWithUID:
1373                     resolved_qual_type = clang::QualType::getFromOpaquePtr(ClangASTContext::AddVolatileModifier (encoding_type->GetOpaqueClangQualType()));
1374                     break;
1375 
1376                 case eTypedefToTypeWithUID:
1377                     resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->CreateClangTypedefType (this, encoding_type));
1378                     // Clear the name so it can get fully qualified in case the
1379                     // typedef is in a namespace.
1380                     m_name.Clear();
1381                     break;
1382 
1383                 case ePointerToTypeWithUID:
1384                     resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->CreateClangPointerType (encoding_type));
1385                     break;
1386 
1387                 case eLValueReferenceToTypeWithUID:
1388                     resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->CreateClangLValueReferenceType (encoding_type));
1389                     break;
1390 
1391                 case eRValueReferenceToTypeWithUID:
1392                     resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->CreateClangRValueReferenceType (encoding_type));
1393                     break;
1394 
1395                 default:
1396                     assert(!"Unhandled encoding_uid_type.");
1397                     break;
1398                 }
1399             }
1400         }
1401         else
1402         {
1403             // We have no encoding type, return void?
1404             void *void_clang_type = type_list->GetClangASTContext().GetVoidBuiltInType();
1405             switch (m_encoding_uid_type)
1406             {
1407             case eIsTypeWithUID:
1408                 resolved_qual_type = clang::QualType::getFromOpaquePtr(void_clang_type);
1409                 break;
1410 
1411             case eIsConstTypeWithUID:
1412                 resolved_qual_type = clang::QualType::getFromOpaquePtr (ClangASTContext::AddConstModifier (void_clang_type));
1413                 break;
1414 
1415             case eIsRestrictTypeWithUID:
1416                 resolved_qual_type = clang::QualType::getFromOpaquePtr (ClangASTContext::AddRestrictModifier (void_clang_type));
1417                 break;
1418 
1419             case eIsVolatileTypeWithUID:
1420                 resolved_qual_type = clang::QualType::getFromOpaquePtr (ClangASTContext::AddVolatileModifier (void_clang_type));
1421                 break;
1422 
1423             case eTypedefToTypeWithUID:
1424                 resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->GetClangASTContext().CreateTypedefType (m_name.AsCString(), void_clang_type, NULL));
1425                 break;
1426 
1427             case ePointerToTypeWithUID:
1428                 resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->GetClangASTContext().CreatePointerType (void_clang_type));
1429                 break;
1430 
1431             case eLValueReferenceToTypeWithUID:
1432                 resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->GetClangASTContext().CreateLValueReferenceType (void_clang_type));
1433                 break;
1434 
1435             case eRValueReferenceToTypeWithUID:
1436                 resolved_qual_type = clang::QualType::getFromOpaquePtr(type_list->GetClangASTContext().CreateRValueReferenceType (void_clang_type));
1437                 break;
1438 
1439             default:
1440                 assert(!"Unhandled encoding_uid_type.");
1441                 break;
1442             }
1443         }
1444         if (resolved_qual_type.getTypePtr())
1445         {
1446             m_clang_qual_type = resolved_qual_type.getAsOpaquePtr();
1447         }
1448 
1449     }
1450     return m_clang_qual_type != NULL;
1451 }
1452 
1453 void *
1454 lldb_private::Type::GetChildClangTypeAtIndex
1455 (
1456     const char *parent_name,
1457     uint32_t idx,
1458     bool transparent_pointers,
1459     bool omit_empty_base_classes,
1460     ConstString& name,
1461     uint32_t &child_byte_size,
1462     int32_t &child_byte_offset,
1463     uint32_t &child_bitfield_bit_size,
1464     uint32_t &child_bitfield_bit_offset
1465 )
1466 {
1467     if (!ResolveClangType())
1468         return false;
1469 
1470     std::string name_str;
1471     void *child_qual_type = GetClangASTContext().GetChildClangTypeAtIndex (
1472             parent_name,
1473             m_clang_qual_type,
1474             idx,
1475             transparent_pointers,
1476             omit_empty_base_classes,
1477             name_str,
1478             child_byte_size,
1479             child_byte_offset,
1480             child_bitfield_bit_size,
1481             child_bitfield_bit_offset);
1482 
1483     if (child_qual_type)
1484     {
1485         if (!name_str.empty())
1486             name.SetCString(name_str.c_str());
1487         else
1488             name.Clear();
1489     }
1490     return child_qual_type;
1491 }
1492 
1493 
1494 
1495 void *
1496 lldb_private::Type::GetOpaqueClangQualType ()
1497 {
1498     ResolveClangType();
1499     return m_clang_qual_type;
1500 }
1501 
1502 clang::ASTContext *
1503 lldb_private::Type::GetClangAST ()
1504 {
1505     TypeList *type_list = GetTypeList();
1506     if (type_list)
1507         return type_list->GetClangASTContext().getASTContext();
1508     return NULL;
1509 }
1510 
1511 lldb_private::ClangASTContext &
1512 lldb_private::Type::GetClangASTContext ()
1513 {
1514     return GetTypeList()->GetClangASTContext();
1515 }
1516 
1517 int
1518 lldb_private::Type::Compare(const Type &a, const Type &b)
1519 {
1520     // Just compare the UID values for now...
1521     lldb::user_id_t a_uid = a.GetID();
1522     lldb::user_id_t b_uid = b.GetID();
1523     if (a_uid < b_uid)
1524         return -1;
1525     if (a_uid > b_uid)
1526         return 1;
1527     return 0;
1528 //  if (a.getQualType() == b.getQualType())
1529 //      return 0;
1530 }
1531 
1532