blob: a5ac2c3805f82ae77d5e98e7d0dd35eb4a71c9dc [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"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Expression/ClangASTSource.h"
14#include "lldb/Expression/ClangExpression.h"
15#include "lldb/Expression/ClangExpressionDeclMap.h"
16
17using namespace clang;
18using namespace lldb_private;
19
Greg Claytonb01000f2011-01-17 03:46:26 +000020ClangASTSource::~ClangASTSource()
21{
22}
Chris Lattner24943d22010-06-08 16:52:24 +000023
Greg Claytonb01000f2011-01-17 03:46:26 +000024void
25ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
26{
Chris Lattner24943d22010-06-08 16:52:24 +000027 // Tell Sema to ask us when looking into the translation unit's decl.
Greg Clayton8de27c72010-10-15 22:48:33 +000028 m_ast_context.getTranslationUnitDecl()->setHasExternalVisibleStorage();
29 m_ast_context.getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000030}
31
Chris Lattner24943d22010-06-08 16:52:24 +000032// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000033DeclContext::lookup_result
34ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000035(
36 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000037 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000038)
39{
Greg Claytonb01000f2011-01-17 03:46:26 +000040 if (m_decl_map.GetImportInProgress())
41 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
42
43 std::string decl_name (clang_decl_name.getAsString());
44
45// if (m_decl_map.DoingASTImport ())
46// return DeclContext::lookup_result();
47//
Greg Clayton8de27c72010-10-15 22:48:33 +000048 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000049 // Normal identifiers.
50 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000051 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
52 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
53 break;
Chris Lattner24943d22010-06-08 16:52:24 +000054
55 // Operator names. Not important for now.
56 case DeclarationName::CXXOperatorName:
57 case DeclarationName::CXXLiteralOperatorName:
58 return DeclContext::lookup_result();
59
60 // Using directives found in this context.
61 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
62 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000063 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000064
65 // These aren't looked up like this.
66 case DeclarationName::ObjCZeroArgSelector:
67 case DeclarationName::ObjCOneArgSelector:
68 case DeclarationName::ObjCMultiArgSelector:
69 return DeclContext::lookup_result();
70
71 // These aren't possible in the global context.
72 case DeclarationName::CXXConstructorName:
73 case DeclarationName::CXXDestructorName:
74 case DeclarationName::CXXConversionFunctionName:
75 return DeclContext::lookup_result();
76 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000077
Greg Claytonf4c7ae02010-10-15 03:36:13 +000078
Greg Clayton8de27c72010-10-15 22:48:33 +000079 if (!m_decl_map.GetLookupsEnabled())
80 {
81 // Wait until we see a '$' at the start of a name before we start doing
82 // any lookups so we can avoid lookup up all of the builtin types.
83 if (!decl_name.empty() && decl_name[0] == '$')
84 {
Sean Callananaa301c42010-12-03 01:38:59 +000085 m_decl_map.SetLookupsEnabled ();
Greg Clayton8de27c72010-10-15 22:48:33 +000086 }
87 else
88 {
89 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
90 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000091 }
Greg Clayton8de27c72010-10-15 22:48:33 +000092
Greg Clayton8de27c72010-10-15 22:48:33 +000093 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +000094
95 const char *uniqued_const_decl_name = const_decl_name.GetCString();
96 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
97 {
98 // We are currently looking up this name...
99 return DeclContext::lookup_result();
100 }
101 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000102// static uint32_t g_depth = 0;
103// ++g_depth;
104// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000105 llvm::SmallVector<NamedDecl*, 4> name_decls;
106 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Greg Clayton8de27c72010-10-15 22:48:33 +0000107 m_decl_map.GetDecls(name_search_context, const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000108 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000109// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000110 m_active_lookups.erase (uniqued_const_decl_name);
111 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000112}
113
Greg Claytonb01000f2011-01-17 03:46:26 +0000114void
115ClangASTSource::CompleteType (TagDecl *tag_decl)
116{
Sean Callanan9b6898f2011-07-30 02:42:06 +0000117 m_decl_map.CompleteTagDecl (tag_decl);
Greg Claytonb01000f2011-01-17 03:46:26 +0000118}
119
120void
121ClangASTSource::CompleteType (ObjCInterfaceDecl *objc_decl)
122{
Sean Callanan9b6898f2011-07-30 02:42:06 +0000123 m_decl_map.CompleteObjCInterfaceDecl (objc_decl);
Greg Claytonb01000f2011-01-17 03:46:26 +0000124}
125
126void
127ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000128{
129 return;
130}
131
Chris Lattner24943d22010-06-08 16:52:24 +0000132// This is used to support iterating through an entire lexical context,
133// which isn't something the debugger should ever need to do.
Sean Callanan9b6898f2011-07-30 02:42:06 +0000134clang::ExternalLoadResult
Greg Claytonb01000f2011-01-17 03:46:26 +0000135ClangASTSource::FindExternalLexicalDecls
136(
137 const DeclContext *DC,
138 bool (*isKindWeWant)(Decl::Kind),
139 llvm::SmallVectorImpl<Decl*> &Decls
140)
141{
Sean Callanan9b6898f2011-07-30 02:42:06 +0000142 return m_decl_map.FindExternalLexicalDecls (DC, isKindWeWant, Decls);
Chris Lattner24943d22010-06-08 16:52:24 +0000143}
144
Greg Claytonb01000f2011-01-17 03:46:26 +0000145clang::ASTContext *
146NameSearchContext::GetASTContext()
147{
Greg Clayton8de27c72010-10-15 22:48:33 +0000148 return &m_ast_source.m_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000149}
150
Greg Claytonb01000f2011-01-17 03:46:26 +0000151clang::NamedDecl *
152NameSearchContext::AddVarDecl(void *type)
153{
Greg Clayton8de27c72010-10-15 22:48:33 +0000154 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000155
156 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000157
Greg Clayton8de27c72010-10-15 22:48:33 +0000158 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
159 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000160 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000161 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000162 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000163 QualType::getFromOpaquePtr(type),
164 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000165 SC_Static,
166 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000167 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000168
169 return Decl;
170}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000171
Greg Claytonb01000f2011-01-17 03:46:26 +0000172clang::NamedDecl *
173NameSearchContext::AddFunDecl (void *type)
174{
Greg Clayton8de27c72010-10-15 22:48:33 +0000175 clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
176 const_cast<DeclContext*>(m_decl_context),
177 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000178 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000179 m_decl_name.getAsIdentifierInfo(),
180 QualType::getFromOpaquePtr(type),
181 NULL,
182 SC_Static,
183 SC_Static,
184 false,
185 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000186
Sean Callananb291abe2010-08-12 23:45:38 +0000187 // We have to do more than just synthesize the FunctionDecl. We have to
188 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
189 // this, we raid the function's FunctionProtoType for types.
190
Greg Clayton8de27c72010-10-15 22:48:33 +0000191 QualType qual_type (QualType::getFromOpaquePtr(type));
192 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000193
Greg Clayton8de27c72010-10-15 22:48:33 +0000194 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000195 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000196 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000197 unsigned ArgIndex;
198
Greg Clayton8de27c72010-10-15 22:48:33 +0000199 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000200
201 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
202 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000203 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000204
Greg Clayton8de27c72010-10-15 22:48:33 +0000205 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
206 const_cast<DeclContext*>(m_decl_context),
207 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000208 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000209 NULL,
210 arg_qual_type,
211 NULL,
212 SC_Static,
213 SC_Static,
214 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000215 }
216
Greg Clayton8de27c72010-10-15 22:48:33 +0000217 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000218
Greg Clayton8de27c72010-10-15 22:48:33 +0000219 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000220 }
221
Greg Clayton8de27c72010-10-15 22:48:33 +0000222 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000223
Greg Clayton8de27c72010-10-15 22:48:33 +0000224 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000225}
Sean Callanan0fc73582010-07-27 00:55:47 +0000226
Greg Claytonb01000f2011-01-17 03:46:26 +0000227clang::NamedDecl *
228NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000229{
Sean Callananad293092011-01-18 23:32:05 +0000230 FunctionProtoType::ExtProtoInfo proto_info;
231
232 proto_info.Variadic = true;
233
Sean Callananfb3058e2011-05-12 23:54:16 +0000234 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.UnknownAnyTy, // result
235 NULL, // argument types
236 0, // number of arguments
Sean Callananad293092011-01-18 23:32:05 +0000237 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000238
Sean Callanan0fc73582010-07-27 00:55:47 +0000239 return AddFunDecl(generic_function_type.getAsOpaquePtr());
240}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000241
Greg Claytonb01000f2011-01-17 03:46:26 +0000242clang::NamedDecl *
243NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000244{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000245 if (type)
246 {
247 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000248
Sean Callanand5b3c352011-01-27 04:42:51 +0000249 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000250 {
251 TagDecl *tag_decl = tag_type->getDecl();
252
253 m_decls.push_back(tag_decl);
254
255 return tag_decl;
256 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000257 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000258 {
259 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
260
261 m_decls.push_back((NamedDecl*)interface_decl);
262
263 return (NamedDecl*)interface_decl;
264 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000265 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000266 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000267}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000268
269void
270NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
271{
272 for (clang::NamedDecl * const *decl_iterator = result.first;
273 decl_iterator != result.second;
274 ++decl_iterator)
275 m_decls.push_back (*decl_iterator);
276}
277
278void
279NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
280{
281 m_decls.push_back (decl);
282}