blob: b18536b5f9e5be986636f2bae1481ab9883e7f59 [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{
117 puts(__PRETTY_FUNCTION__);
118}
119
120void
121ClangASTSource::CompleteType (ObjCInterfaceDecl *objc_decl)
122{
123 puts(__PRETTY_FUNCTION__);
124}
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.
Greg Claytonb01000f2011-01-17 03:46:26 +0000134bool
135ClangASTSource::FindExternalLexicalDecls
136(
137 const DeclContext *DC,
138 bool (*isKindWeWant)(Decl::Kind),
139 llvm::SmallVectorImpl<Decl*> &Decls
140)
141{
Chris Lattner24943d22010-06-08 16:52:24 +0000142 // true is for error, that's good enough for me
143 return true;
144}
145
Greg Claytonb01000f2011-01-17 03:46:26 +0000146clang::ASTContext *
147NameSearchContext::GetASTContext()
148{
Greg Clayton8de27c72010-10-15 22:48:33 +0000149 return &m_ast_source.m_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000150}
151
Greg Claytonb01000f2011-01-17 03:46:26 +0000152clang::NamedDecl *
153NameSearchContext::AddVarDecl(void *type)
154{
Greg Clayton8de27c72010-10-15 22:48:33 +0000155 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanan1ddd9fe2010-11-30 00:27:43 +0000156
157 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000158
Greg Clayton8de27c72010-10-15 22:48:33 +0000159 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
160 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +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(),
178 m_decl_name.getAsIdentifierInfo(),
179 QualType::getFromOpaquePtr(type),
180 NULL,
181 SC_Static,
182 SC_Static,
183 false,
184 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000185
Sean Callananb291abe2010-08-12 23:45:38 +0000186 // We have to do more than just synthesize the FunctionDecl. We have to
187 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
188 // this, we raid the function's FunctionProtoType for types.
189
Greg Clayton8de27c72010-10-15 22:48:33 +0000190 QualType qual_type (QualType::getFromOpaquePtr(type));
191 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000192
Greg Clayton8de27c72010-10-15 22:48:33 +0000193 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000194 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000195 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000196 unsigned ArgIndex;
197
Greg Clayton8de27c72010-10-15 22:48:33 +0000198 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000199
200 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
201 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000202 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000203
Greg Clayton8de27c72010-10-15 22:48:33 +0000204 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
205 const_cast<DeclContext*>(m_decl_context),
206 SourceLocation(),
207 NULL,
208 arg_qual_type,
209 NULL,
210 SC_Static,
211 SC_Static,
212 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000213 }
214
Greg Clayton8de27c72010-10-15 22:48:33 +0000215 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000216
Greg Clayton8de27c72010-10-15 22:48:33 +0000217 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000218 }
219
Greg Clayton8de27c72010-10-15 22:48:33 +0000220 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000221
Greg Clayton8de27c72010-10-15 22:48:33 +0000222 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000223}
Sean Callanan0fc73582010-07-27 00:55:47 +0000224
Greg Claytonb01000f2011-01-17 03:46:26 +0000225clang::NamedDecl *
226NameSearchContext::AddGenericFunDecl()
Sean Callanan0fc73582010-07-27 00:55:47 +0000227{
Sean Callananad293092011-01-18 23:32:05 +0000228 FunctionProtoType::ExtProtoInfo proto_info;
229
230 proto_info.Variadic = true;
231
Greg Clayton8de27c72010-10-15 22:48:33 +0000232 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
Sean Callananad293092011-01-18 23:32:05 +0000233 NULL, // argument types
234 0, // number of arguments
235 proto_info));
Greg Clayton8de27c72010-10-15 22:48:33 +0000236
Sean Callanan0fc73582010-07-27 00:55:47 +0000237 return AddFunDecl(generic_function_type.getAsOpaquePtr());
238}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000239
Greg Claytonb01000f2011-01-17 03:46:26 +0000240clang::NamedDecl *
241NameSearchContext::AddTypeDecl(void *type)
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000242{
Greg Clayton8de27c72010-10-15 22:48:33 +0000243 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000244
Greg Clayton8de27c72010-10-15 22:48:33 +0000245 if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000246 {
247 TagDecl *tag_decl = tag_type->getDecl();
248
Greg Clayton8de27c72010-10-15 22:48:33 +0000249 m_decls.push_back(tag_decl);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000250
251 return tag_decl;
252 }
Sean Callanan3aa7da52010-12-13 22:46:15 +0000253 else if (ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
254 {
255 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
256
257 m_decls.push_back((NamedDecl*)interface_decl);
258
259 return (NamedDecl*)interface_decl;
260 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000261 else
262 {
263 return NULL;
264 }
265}