TypeList.cpp revision 24943d2ee8bfaa7cf5893e4709143924157a5c1e
1//===-- TypeList.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
11// C Includes
12// C++ Includes
13#include <vector>
14
15// Other libraries and framework includes
16#include "clang/AST/ASTConsumer.h"
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclCXX.h"
20#include "clang/AST/DeclGroup.h"
21
22#include "clang/Basic/Builtins.h"
23#include "clang/Basic/IdentifierTable.h"
24#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/SourceManager.h"
26#include "clang/Basic/TargetInfo.h"
27
28#include "llvm/Support/FormattedStream.h"
29#include "llvm/Support/raw_ostream.h"
30
31// Project includes
32#include "lldb/Symbol/ClangASTContext.h"
33#include "lldb/Symbol/SymbolFile.h"
34#include "lldb/Symbol/SymbolVendor.h"
35#include "lldb/Symbol/Type.h"
36#include "lldb/Symbol/TypeList.h"
37
38using namespace lldb;
39using namespace lldb_private;
40using namespace clang;
41
42TypeList::TypeList(const char *target_triple) :
43    m_types(),
44    m_ast(target_triple)
45{
46}
47
48//----------------------------------------------------------------------
49// Destructor
50//----------------------------------------------------------------------
51TypeList::~TypeList()
52{
53}
54
55//----------------------------------------------------------------------
56// Add a base type to the type list
57//----------------------------------------------------------------------
58
59//struct CampareDCTypeBaton
60//{
61//  CampareDCTypeBaton(const std::vector<TypeSP>& _types, const Type* _search_type) :
62//      types(_types),
63//      search_type(_search_type)
64//  {
65//  }
66//  const std::vector<TypeSP>& types;
67//  const Type* search_type;
68//};
69//
70//static int
71//compare_dc_type (const void *key, const void *arrmem)
72//{
73//  const Type* search_type = ((CampareDCTypeBaton*) key)->search_type;
74//  uint32_t curr_index = *(uint32_t *)arrmem;
75//  const Type* curr_type = ((CampareDCTypeBaton*) key)->types[curr_index].get();
76//  Type::CompareState state;
77//  return Type::Compare(*search_type, *curr_type, state);
78//}
79//
80//struct LessThanBinaryPredicate
81//{
82//  LessThanBinaryPredicate(const CampareDCTypeBaton& _compare_baton) :
83//      compare_baton(_compare_baton)
84//  {
85//  }
86//
87//  bool operator() (uint32_t a, uint32_t b) const
88//  {
89//      Type::CompareState state;
90//      return Type::Compare(*compare_baton.search_type, *compare_baton.types[b].get(), state) < 0;
91//  }
92//  const CampareDCTypeBaton& compare_baton;
93//};
94
95TypeSP
96TypeList::InsertUnique(TypeSP& type_sp)
97{
98#if 0
99//  Stream s(stdout);
100//  s << "TypeList::InsertUnique for type ";
101//  type_sp->Dump(s);
102//  s << "Current list:\n";
103//  Dump(s);
104
105    CampareDCTypeBaton compare_baton(m_types, type_sp.get());
106    uint32_t* match_index_ptr = (uint32_t*)bsearch(&compare_baton, &m_sorted_indexes[0], m_sorted_indexes.size(), sizeof(uint32_t), compare_dc_type);
107    if (match_index_ptr)
108    {
109//      s << "returning existing type: " << (void *)m_types[*match_index_ptr].get() << "\n";
110        return m_types[*match_index_ptr];
111    }
112
113    // Get the new index within the m_types array before we add the new type
114    uint32_t uniqued_type_index = m_types.size();
115    // Add the new shared pointer to our type by appending it to the end of the types array
116    m_types.push_back(type_sp);
117    // Figure out what the sorted index of this new type should be
118    uint32_t fake_index = 0;
119    LessThanBinaryPredicate compare_func_obj(compare_baton);
120    std::vector<uint32_t>::iterator insert_pos = std::upper_bound(m_sorted_indexes.begin(), m_sorted_indexes.end(), fake_index, compare_func_obj);
121    // Insert the sorted index into our sorted index array
122    m_sorted_indexes.insert(insert_pos, uniqued_type_index);
123#else
124    // Just push each type on the back for now. We will worry about uniquing later
125    m_types.push_back (type_sp);
126#endif
127//  s << "New list:\n";
128//  Dump(s);
129
130    return type_sp;
131}
132
133//----------------------------------------------------------------------
134// Find a base type by its unique ID.
135//----------------------------------------------------------------------
136TypeSP
137TypeList::FindType(lldb::user_id_t uid)
138{
139    TypeSP type_sp;
140    iterator pos, end;
141    for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
142        if ((*pos)->GetID() == uid)
143            return *pos;
144
145    return type_sp;
146}
147
148//----------------------------------------------------------------------
149// Find a type by name.
150//----------------------------------------------------------------------
151TypeList
152TypeList::FindTypes(const ConstString &name)
153{
154    TypeList types(m_ast.getTargetInfo()->getTriple().getTriple().c_str());
155    iterator pos, end;
156    for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos)
157        if ((*pos)->GetName() == name)
158            types.InsertUnique(*pos);
159    return types;
160}
161
162void
163TypeList::Clear()
164{
165    m_types.clear();
166}
167
168uint32_t
169TypeList::GetSize() const
170{
171    return m_types.size();
172}
173
174TypeSP
175TypeList::GetTypeAtIndex(uint32_t idx)
176{
177    TypeSP type_sp;
178    if (idx < m_types.size())
179        type_sp = m_types[idx];
180    return type_sp;
181}
182
183void
184TypeList::Dump(Stream *s, bool show_context)
185{
186//  std::vector<uint32_t>::const_iterator pos, end;
187//  for (pos = end = m_sorted_indexes.begin(), end = m_sorted_indexes.end(); pos != end; ++pos)
188//  {
189//      m_types[*pos]->Dump(s, show_context);
190//  }
191
192    m_ast.getASTContext()->getTranslationUnitDecl()->print(llvm::fouts(), 0);
193    const size_t num_types = m_types.size();
194    for (size_t i=0; i<num_types; ++i)
195    {
196        m_types[i]->Dump(s, show_context);
197    }
198//  ASTContext *ast_context = GetClangASTContext ().getASTContext();
199//  if (ast_context)
200//      ast_context->PrintStats();
201}
202
203
204ClangASTContext &
205TypeList::GetClangASTContext ()
206{
207    return m_ast;
208}
209
210void *
211TypeList::CreateClangPointerType (Type *type)
212{
213    assert(type);
214    return m_ast.CreatePointerType(type->GetOpaqueClangQualType());
215}
216
217void *
218TypeList::CreateClangTypedefType (Type *typedef_type, Type *base_type)
219{
220    assert(typedef_type && base_type);
221    return m_ast.CreateTypedefType(typedef_type->GetName().AsCString(), base_type->GetOpaqueClangQualType(), typedef_type->GetSymbolFile()->GetClangDeclContextForTypeUID(typedef_type->GetID()));
222}
223
224void *
225TypeList::CreateClangLValueReferenceType (Type *type)
226{
227    assert(type);
228    return m_ast.CreateLValueReferenceType(type->GetOpaqueClangQualType());
229}
230
231void *
232TypeList::CreateClangRValueReferenceType (Type *type)
233{
234    assert(type);
235    return m_ast.CreateRValueReferenceType (type->GetOpaqueClangQualType());
236}
237
238
239
240