blob: 858bdf0737edade4e9e94303a960cbbbf87418e1 [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"
Chris Lattner24943d22010-06-08 16:52:24 +000014#include "lldb/Expression/ClangASTSource.h"
15#include "lldb/Expression/ClangExpression.h"
16#include "lldb/Expression/ClangExpressionDeclMap.h"
17
18using namespace clang;
19using namespace lldb_private;
20
Greg Claytonb01000f2011-01-17 03:46:26 +000021ClangASTSource::~ClangASTSource()
22{
23}
Chris Lattner24943d22010-06-08 16:52:24 +000024
Greg Claytonb01000f2011-01-17 03:46:26 +000025void
26ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer)
27{
Sean Callananf76afff2011-10-28 23:38:38 +000028 if (!m_ast_context)
29 return;
30
31 m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage();
32 m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000033}
34
Chris Lattner24943d22010-06-08 16:52:24 +000035// The core lookup interface.
Greg Claytonb01000f2011-01-17 03:46:26 +000036DeclContext::lookup_result
37ClangASTSource::FindExternalVisibleDeclsByName
Greg Claytonf4c7ae02010-10-15 03:36:13 +000038(
39 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000040 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000041)
42{
Sean Callananf76afff2011-10-28 23:38:38 +000043 if (!m_ast_context)
44 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
45
46 if (GetImportInProgress())
Greg Claytonb01000f2011-01-17 03:46:26 +000047 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
48
49 std::string decl_name (clang_decl_name.getAsString());
50
51// if (m_decl_map.DoingASTImport ())
52// return DeclContext::lookup_result();
53//
Greg Clayton8de27c72010-10-15 22:48:33 +000054 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000055 // Normal identifiers.
56 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000057 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
58 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
59 break;
Chris Lattner24943d22010-06-08 16:52:24 +000060
61 // Operator names. Not important for now.
62 case DeclarationName::CXXOperatorName:
63 case DeclarationName::CXXLiteralOperatorName:
64 return DeclContext::lookup_result();
65
66 // Using directives found in this context.
67 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
68 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000069 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000070
71 // These aren't looked up like this.
72 case DeclarationName::ObjCZeroArgSelector:
73 case DeclarationName::ObjCOneArgSelector:
74 case DeclarationName::ObjCMultiArgSelector:
75 return DeclContext::lookup_result();
76
77 // These aren't possible in the global context.
78 case DeclarationName::CXXConstructorName:
79 case DeclarationName::CXXDestructorName:
80 case DeclarationName::CXXConversionFunctionName:
81 return DeclContext::lookup_result();
82 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000083
Greg Claytonf4c7ae02010-10-15 03:36:13 +000084
Sean Callananf76afff2011-10-28 23:38:38 +000085 if (!GetLookupsEnabled())
Greg Clayton8de27c72010-10-15 22:48:33 +000086 {
87 // Wait until we see a '$' at the start of a name before we start doing
88 // any lookups so we can avoid lookup up all of the builtin types.
89 if (!decl_name.empty() && decl_name[0] == '$')
90 {
Sean Callananf76afff2011-10-28 23:38:38 +000091 SetLookupsEnabled (true);
Greg Clayton8de27c72010-10-15 22:48:33 +000092 }
93 else
94 {
95 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
96 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000097 }
Greg Clayton8de27c72010-10-15 22:48:33 +000098
Greg Clayton8de27c72010-10-15 22:48:33 +000099 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000100
101 const char *uniqued_const_decl_name = const_decl_name.GetCString();
102 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
103 {
104 // We are currently looking up this name...
105 return DeclContext::lookup_result();
106 }
107 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000108// static uint32_t g_depth = 0;
109// ++g_depth;
110// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000111 llvm::SmallVector<NamedDecl*, 4> name_decls;
112 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Sean Callananf76afff2011-10-28 23:38:38 +0000113 FindExternalVisibleDecls(name_search_context);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000114 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000115// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000116 m_active_lookups.erase (uniqued_const_decl_name);
117 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000118}
119
Greg Claytonb01000f2011-01-17 03:46:26 +0000120void
Sean Callananf76afff2011-10-28 23:38:38 +0000121ClangASTSource::FindExternalVisibleDecls (NameSearchContext &context)
122{
123}
124
125void
Greg Claytonb01000f2011-01-17 03:46:26 +0000126ClangASTSource::CompleteType (TagDecl *tag_decl)
127{
Greg Claytonb01000f2011-01-17 03:46:26 +0000128}
129
130void
131ClangASTSource::CompleteType (ObjCInterfaceDecl *objc_decl)
132{
Greg Claytonb01000f2011-01-17 03:46:26 +0000133}
134
Sean Callanan9b6898f2011-07-30 02:42:06 +0000135clang::ExternalLoadResult
Greg Claytonb01000f2011-01-17 03:46:26 +0000136ClangASTSource::FindExternalLexicalDecls
137(
138 const DeclContext *DC,
139 bool (*isKindWeWant)(Decl::Kind),
140 llvm::SmallVectorImpl<Decl*> &Decls
141)
142{
Sean Callananf76afff2011-10-28 23:38:38 +0000143 return ELR_Success;
Chris Lattner24943d22010-06-08 16:52:24 +0000144}
145
Greg Claytonb01000f2011-01-17 03:46:26 +0000146clang::NamedDecl *
147NameSearchContext::AddVarDecl(void *type)
148{
Greg Clayton8de27c72010-10-15 22:48:33 +0000149 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000150
151 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000152
Sean Callananf76afff2011-10-28 23:38:38 +0000153 clang::NamedDecl *Decl = VarDecl::Create(*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000154 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000155 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000156 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000157 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000158 QualType::getFromOpaquePtr(type),
159 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000160 SC_Static,
161 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000162 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000163
164 return Decl;
165}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000166
Greg Claytonb01000f2011-01-17 03:46:26 +0000167clang::NamedDecl *
168NameSearchContext::AddFunDecl (void *type)
169{
Sean Callananf76afff2011-10-28 23:38:38 +0000170 clang::FunctionDecl *func_decl = FunctionDecl::Create (*m_ast_source.m_ast_context,
Greg Clayton8de27c72010-10-15 22:48:33 +0000171 const_cast<DeclContext*>(m_decl_context),
172 SourceLocation(),
Sean Callanan279584c2011-03-15 00:17:19 +0000173 SourceLocation(),
Greg Clayton8de27c72010-10-15 22:48:33 +0000174 m_decl_name.getAsIdentifierInfo(),
175 QualType::getFromOpaquePtr(type),
176 NULL,
177 SC_Static,
178 SC_Static,
179 false,
180 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000181
Sean Callananb291abe2010-08-12 23:45:38 +0000182 // We have to do more than just synthesize the FunctionDecl. We have to
183 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
184 // this, we raid the function's FunctionProtoType for types.
185
Greg Clayton8de27c72010-10-15 22:48:33 +0000186 QualType qual_type (QualType::getFromOpaquePtr(type));
187 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000188
Greg Clayton8de27c72010-10-15 22:48:33 +0000189 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000190 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000191 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000192 unsigned ArgIndex;
193
Sean Callananc1535182011-10-07 23:18:13 +0000194 SmallVector<ParmVarDecl *, 5> parm_var_decls;
195
Sean Callanan8f0dc342010-06-22 23:46:24 +0000196 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
197 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000198 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000199
Sean Callananf76afff2011-10-28 23:38:38 +0000200 parm_var_decls.push_back(ParmVarDecl::Create (*m_ast_source.m_ast_context,
Sean Callananc1535182011-10-07 23:18:13 +0000201 const_cast<DeclContext*>(m_decl_context),
202 SourceLocation(),
203 SourceLocation(),
204 NULL,
205 arg_qual_type,
206 NULL,
207 SC_Static,
208 SC_Static,
209 NULL));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000210 }
211
Sean Callananc1535182011-10-07 23:18:13 +0000212 func_decl->setParams(ArrayRef<ParmVarDecl*>(parm_var_decls));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000213 }
214
Greg Clayton8de27c72010-10-15 22:48:33 +0000215 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000216
Greg Clayton8de27c72010-10-15 22:48:33 +0000217 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000218}
Sean Callanan0fc73582010-07-27 00:55:47 +0000219
Greg Claytonb01000f2011-01-17 03:46:26 +0000220clang::NamedDecl *
221NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000222{
Sean Callananad293092011-01-18 23:32:05 +0000223 FunctionProtoType::ExtProtoInfo proto_info;
224
225 proto_info.Variadic = true;
226
Sean Callananf76afff2011-10-28 23:38:38 +0000227 QualType generic_function_type(m_ast_source.m_ast_context->getFunctionType (m_ast_source.m_ast_context->UnknownAnyTy, // result
228 NULL, // argument types
229 0, // number of arguments
230 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000231
Sean Callanan0fc73582010-07-27 00:55:47 +0000232 return AddFunDecl(generic_function_type.getAsOpaquePtr());
233}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000234
Greg Claytonb01000f2011-01-17 03:46:26 +0000235clang::NamedDecl *
236NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000237{
Greg Claytona1aaaff2011-01-23 00:34:52 +0000238 if (type)
239 {
240 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000241
Sean Callanand5b3c352011-01-27 04:42:51 +0000242 if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000243 {
244 TagDecl *tag_decl = tag_type->getDecl();
245
246 m_decls.push_back(tag_decl);
247
248 return tag_decl;
249 }
Sean Callanand5b3c352011-01-27 04:42:51 +0000250 else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
Greg Claytona1aaaff2011-01-23 00:34:52 +0000251 {
252 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
253
254 m_decls.push_back((NamedDecl*)interface_decl);
255
256 return (NamedDecl*)interface_decl;
257 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000258 }
Greg Claytona1aaaff2011-01-23 00:34:52 +0000259 return NULL;
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000260}
Greg Claytone6d72ca2011-06-25 00:44:06 +0000261
262void
263NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result)
264{
265 for (clang::NamedDecl * const *decl_iterator = result.first;
266 decl_iterator != result.second;
267 ++decl_iterator)
268 m_decls.push_back (*decl_iterator);
269}
270
271void
272NameSearchContext::AddNamedDecl (clang::NamedDecl *decl)
273{
274 m_decls.push_back (decl);
275}