blob: 7ed5a5abcb646348e8a575982965fdaf4607b208 [file] [log] [blame]
Greg Clayton1e591ce2010-07-16 18:28:27 +00001//===-- ClangASTSource.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
Chris Lattner24943d22010-06-08 16:52:24 +000010
Chris Lattner24943d22010-06-08 16:52:24 +000011#include "clang/AST/ASTContext.h"
Greg Claytonf4c7ae02010-10-15 03:36:13 +000012#include "lldb/Core/Log.h"
Greg Clayton6e0101c2011-09-17 06:21:20 +000013#include "lldb/Core/Module.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000014#include "lldb/Core/ModuleList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000015#include "lldb/Expression/ClangASTSource.h"
16#include "lldb/Expression/ClangExpression.h"
17#include "lldb/Expression/ClangExpressionDeclMap.h"
Sean Callanan73b520f2011-10-29 01:58:46 +000018#include "lldb/Symbol/ClangNamespaceDecl.h"
19#include "lldb/Symbol/SymbolVendor.h"
20#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22using namespace clang;
23using namespace lldb_private;
24
Greg Claytonb01000f2011-01-17 03:46:26 +000025ClangASTSource::~ClangASTSource()
26{
27}
Chris Lattner24943d22010-06-08 16:52:24 +000028
Greg Claytonb01000f2011-01-17 03:46:26 +000029void
30ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
31{
Sean Callananf76afff2011-10-28 23:38:38 +000032 if (!m_ast_context)
33 return;
34
35 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
36 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000037}
38
Chris Lattner24943d22010-06-08 16:52:24 +000039// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000040DeclContext::lookup_result
41ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000042(
43 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000044 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000045)
46{
Sean Callananf76afff2011-10-28 23:38:38 +000047 if (!m_ast_context)
48 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
49
50 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000051 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
52
53 std::string decl_name (clang_decl_name.getAsString());
54
55// if (m_decl_map.DoingASTImport ())
56// return DeclContext::lookup_result();
57//
Greg Clayton8de27c72010-10-15 22:48:33 +000058 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000059 // Normal identifiers.
60 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000061 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
62 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
63 break;
Chris Lattner24943d22010-06-08 16:52:24 +000064
65 // Operator names. Not important for now.
66 case DeclarationName::CXXOperatorName:
67 case DeclarationName::CXXLiteralOperatorName:
68 return DeclContext::lookup_result();
69
70 // Using directives found in this context.
71 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
72 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000073 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000074
75 // These aren't looked up like this.
76 case DeclarationName::ObjCZeroArgSelector:
77 case DeclarationName::ObjCOneArgSelector:
78 case DeclarationName::ObjCMultiArgSelector:
79 return DeclContext::lookup_result();
80
81 // These aren't possible in the global context.
82 case DeclarationName::CXXConstructorName:
83 case DeclarationName::CXXDestructorName:
84 case DeclarationName::CXXConversionFunctionName:
85 return DeclContext::lookup_result();
86 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000087
Greg Claytonf4c7ae02010-10-15 03:36:13 +000088
Sean Callananf76afff2011-10-28 23:38:38 +000089 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +000090 {
91 // Wait until we see a '$' at the start of a name before we start doing
92 // any lookups so we can avoid lookup up all of the builtin types.
93 if (!decl_name.empty() && decl_name[0] == '$')
94 {
Sean Callananf76afff2011-10-28 23:38:38 +000095 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +000096 }
97 else
98 {
99 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
100 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +0000101 }
Greg Clayton8de27c72010-10-15 22:48:33 +0000102
Greg Clayton8de27c72010-10-15 22:48:33 +0000103 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000104
105 const char *uniqued_const_decl_name = const_decl_name.GetCString();
106 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
107 {
108 // We are currently looking up this name...
109 return DeclContext::lookup_result();
110 }
111 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000112// static uint32_t g_depth = 0;
113// ++g_depth;
114// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000115 llvm::SmallVector<NamedDecl*, 4> name_decls;
116 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000117 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000118 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000119// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000120 m_active_lookups.erase (uniqued_const_decl_name);
121 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000122}
123
Greg Claytonb01000f2011-01-17 03:46:26 +0000124void
Sean Callananf76afff2011-10-28 23:38:38 +0000125ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
126{
127}
128
129void
Greg Claytonb01000f2011-01-17 03:46:26 +0000130ClangASTSource::CompleteType (TagDecl *tag_decl)
131{
Greg Claytonb01000f2011-01-17 03:46:26 +0000132}
133
134void
135ClangASTSource::CompleteType (ObjCInterfaceDecl *objc_decl)
136{
Greg Claytonb01000f2011-01-17 03:46:26 +0000137}
138
Sean Callanan9b6898f2011-07-30 02:42:06 +0000139clang::ExternalLoadResult
Greg Claytonb01000f2011-01-17 03:46:26 +0000140ClangASTSource::FindExternalLexicalDecls
141(
142 const DeclContext *DC,
143 bool (*isKindWeWant)(Decl::Kind),
144 llvm::SmallVectorImpl<Decl*> &Decls
145)
146{
Sean Callananf76afff2011-10-28 23:38:38 +0000147 return ELR_Success;
Chris Lattner24943d22010-06-08 16:52:24 +0000148}
149
Sean Callanan73b520f2011-10-29 01:58:46 +0000150void
151ClangASTSource::CompleteNamespaceMap (ClangASTImporter::NamespaceMapSP &namespace_map,
152 const ConstString &name,
153 ClangASTImporter::NamespaceMapSP &parent_map) const
154{
155 static unsigned int invocation_id = 0;
156 unsigned int current_id = invocation_id++;
157
158 lldb::LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
159
160 if (log)
161 {
162 if (parent_map && parent_map->size())
163 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s in namespace %s",
164 current_id,
165 name.GetCString(),
166 parent_map->begin()->second.GetNamespaceDecl()->getDeclName().getAsString().c_str());
167 else
168 log->Printf("CompleteNamespaceMap[%u] Searching for namespace %s",
169 current_id,
170 name.GetCString());
171 }
172
173
174 if (parent_map)
175 {
176 for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), e = parent_map->end();
177 i != e;
178 ++i)
179 {
180 ClangNamespaceDecl found_namespace_decl;
181
182 lldb::ModuleSP module_sp = i->first;
183 ClangNamespaceDecl module_parent_namespace_decl = i->second;
184
185 SymbolVendor *symbol_vendor = module_sp->GetSymbolVendor();
186
187 if (!symbol_vendor)
188 continue;
189
190 SymbolContext null_sc;
191
192 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &module_parent_namespace_decl);
193
194 if (!found_namespace_decl)
195 continue;
196
197 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(module_sp, found_namespace_decl));
198
199 if (log)
200 log->Printf(" CMN[%u] Found namespace %s in module %s",
201 current_id,
202 name.GetCString(),
203 module_sp->GetFileSpec().GetFilename().GetCString());
204 }
205 }
206 else
207 {
208 ModuleList &images = m_target->GetImages();
209 ClangNamespaceDecl null_namespace_decl;
210
211 for (uint32_t i = 0, e = images.GetSize();
212 i != e;
213 ++i)
214 {
215 lldb::ModuleSP image = images.GetModuleAtIndex(i);
216
217 if (!image)
218 continue;
219
220 ClangNamespaceDecl found_namespace_decl;
221
222 SymbolVendor *symbol_vendor = image->GetSymbolVendor();
223
224 if (!symbol_vendor)
225 continue;
226
227 SymbolContext null_sc;
228
229 found_namespace_decl = symbol_vendor->FindNamespace(null_sc, name, &null_namespace_decl);
230
231 if (!found_namespace_decl)
232 continue;
233
234 namespace_map->push_back(std::pair<lldb::ModuleSP, ClangNamespaceDecl>(image, found_namespace_decl));
235
236 if (log)
237 log->Printf(" CMN[%u] Found namespace %s in module %s",
238 current_id,
239 name.GetCString(),
240 image->GetFileSpec().GetFilename().GetCString());
241 }
242 }
243}
244
Greg Claytonb01000f2011-01-17 03:46:26 +0000245clang::NamedDecl *
246NameSearchContext::AddVarDecl(void *type)
247{
Greg Clayton8de27c72010-10-15 22:48:33 +0000248 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000249
250 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000251
Sean Callananf76afff2011-10-28 23:38:38 +0000252 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000253 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000254 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000255 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000256 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000257 QualType::getFromOpaquePtr(type),
258 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000259 SC_Static,
260 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000261 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000262
263 return Decl;
264}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000265
Greg Claytonb01000f2011-01-17 03:46:26 +0000266clang::NamedDecl *
267NameSearchContext::AddFunDecl (void *type)
268{
Sean Callananf76afff2011-10-28 23:38:38 +0000269 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000270 const_cast<DeclContext*>(m_decl_context),
271 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000272 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000273 m_decl_name.getAsIdentifierInfo(),
274 QualType::getFromOpaquePtr(type),
275 NULL,
276 SC_Static,
277 SC_Static,
278 false,
279 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000280
Sean Callananb291abe2010-08-12 23:45:38 +0000281 // We have to do more than just synthesize the FunctionDecl. We have to
282 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
283 // this, we raid the function's FunctionProtoType for types.
284
Greg Clayton8de27c72010-10-15 22:48:33 +0000285 QualType qual_type (QualType::getFromOpaquePtr(type));
286 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000287
Greg Clayton8de27c72010-10-15 22:48:33 +0000288 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000289 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000290 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000291 unsigned ArgIndex;
292
Sean Callananc1535182011-10-07 23:18:13 +0000293 SmallVector<ParmVarDecl *, 5> parm_var_decls;
294
Sean Callanan8f0dc342010-06-22 23:46:24 +0000295 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
296 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000297 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000298
Sean Callananf76afff2011-10-28 23:38:38 +0000299 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000300 const_cast<DeclContext*>(m_decl_context),
301 SourceLocation(),
302 SourceLocation(),
303 NULL,
304 arg_qual_type,
305 NULL,
306 SC_Static,
307 SC_Static,
308 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000309 }
310
Sean Callananc1535182011-10-07 23:18:13 +0000311 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000312 }
313
Greg Clayton8de27c72010-10-15 22:48:33 +0000314 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000315
Greg Clayton8de27c72010-10-15 22:48:33 +0000316 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000317}
Sean Callanan0fc73582010-07-27 00:55:47 +0000318
Greg Claytonb01000f2011-01-17 03:46:26 +0000319clang::NamedDecl *
320NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000321{
Sean Callananad293092011-01-18 23:32:05 +0000322 FunctionProtoType::ExtProtoInfo proto_info;
323
324 proto_info.Variadic = true;
325
Sean Callananf76afff2011-10-28 23:38:38 +0000326 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
327 NULL, // argument types
328 0, // number of arguments
329 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000330
Sean Callanan0fc73582010-07-27 00:55:47 +0000331 return AddFunDecl(generic_function_type.getAsOpaquePtr());
332}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000333
Greg Claytonb01000f2011-01-17 03:46:26 +0000334clang::NamedDecl *
335NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000336{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000337 if (type)
338 {
339 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000340
Sean Callanand5b3c352011-01-27 04:42:51 +0000341 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000342 {
343 TagDecl *tag_decl = tag_type->getDecl();
344
345 m_decls.push_back(tag_decl);
346
347 return tag_decl;
348 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000349 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000350 {
351 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
352
353 m_decls.push_back((NamedDecl*)interface_decl);
354
355 return (NamedDecl*)interface_decl;
356 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000357 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000358 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000359}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000360
361void
362NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
363{
364 for (clang::NamedDecl * const *decl_iterator = result.first;
365 decl_iterator != result.second;
366 ++decl_iterator)
367 m_decls.push_back (*decl_iterator);
368}
369
370void
371NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
372{
373 m_decls.push_back (decl);
374}