blob: 330c1da8129386cdbedb901caf3d543bf0c8425f [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
20ClangASTSource::~ClangASTSource() {}
21
22void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
23 // Tell Sema to ask us when looking into the translation unit's decl.
Greg Clayton8de27c72010-10-15 22:48:33 +000024 m_ast_context.getTranslationUnitDecl()->setHasExternalVisibleStorage();
25 m_ast_context.getTranslationUnitDecl()->setHasExternalLexicalStorage();
Chris Lattner24943d22010-06-08 16:52:24 +000026}
27
28// These are only required for AST source that want to lazily load
29// the declarations (or parts thereof) that they return.
30Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; }
31Stmt *ClangASTSource::GetExternalDeclStmt(uint64_t) { return 0; }
32
33// These are also optional, although it might help with ObjC
34// debugging if we have respectable signatures. But a more
35// efficient interface (that didn't require scanning all files
36// for method signatures!) might help.
37Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); }
38uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; }
39
40// The core lookup interface.
Greg Claytonf4c7ae02010-10-15 03:36:13 +000041DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName
42(
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{
Greg Clayton8de27c72010-10-15 22:48:33 +000047 switch (clang_decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000048 // Normal identifiers.
49 case DeclarationName::Identifier:
Greg Clayton8de27c72010-10-15 22:48:33 +000050 if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0)
51 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
52 break;
Chris Lattner24943d22010-06-08 16:52:24 +000053
54 // Operator names. Not important for now.
55 case DeclarationName::CXXOperatorName:
56 case DeclarationName::CXXLiteralOperatorName:
57 return DeclContext::lookup_result();
58
59 // Using directives found in this context.
60 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
61 case DeclarationName::CXXUsingDirective:
Greg Clayton8de27c72010-10-15 22:48:33 +000062 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000063
64 // These aren't looked up like this.
65 case DeclarationName::ObjCZeroArgSelector:
66 case DeclarationName::ObjCOneArgSelector:
67 case DeclarationName::ObjCMultiArgSelector:
68 return DeclContext::lookup_result();
69
70 // These aren't possible in the global context.
71 case DeclarationName::CXXConstructorName:
72 case DeclarationName::CXXDestructorName:
73 case DeclarationName::CXXConversionFunctionName:
74 return DeclContext::lookup_result();
75 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000076
Greg Clayton8de27c72010-10-15 22:48:33 +000077 std::string decl_name (clang_decl_name.getAsString());
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 {
85 m_decl_map.SetLookupsEnabled (true);
86 }
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
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000114void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
115{
116 return;
117}
118
Chris Lattner24943d22010-06-08 16:52:24 +0000119// This is used to support iterating through an entire lexical context,
120// which isn't something the debugger should ever need to do.
Sean Callanan8950c9a2010-10-29 18:38:40 +0000121bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC,
122 bool (*isKindWeWant)(Decl::Kind),
123 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattner24943d22010-06-08 16:52:24 +0000124 // true is for error, that's good enough for me
125 return true;
126}
127
128clang::ASTContext *NameSearchContext::GetASTContext() {
Greg Clayton8de27c72010-10-15 22:48:33 +0000129 return &m_ast_source.m_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000130}
131
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000132clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
Greg Clayton8de27c72010-10-15 22:48:33 +0000133 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanancc074622010-09-14 21:59:34 +0000134
Greg Clayton8de27c72010-10-15 22:48:33 +0000135 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
136 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000137 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000138 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000139 QualType::getFromOpaquePtr(type),
140 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000141 SC_Static,
142 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000143 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000144
145 return Decl;
146}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000147
Greg Clayton8de27c72010-10-15 22:48:33 +0000148clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
149 clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
150 const_cast<DeclContext*>(m_decl_context),
151 SourceLocation(),
152 m_decl_name.getAsIdentifierInfo(),
153 QualType::getFromOpaquePtr(type),
154 NULL,
155 SC_Static,
156 SC_Static,
157 false,
158 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000159
Sean Callananb291abe2010-08-12 23:45:38 +0000160 // We have to do more than just synthesize the FunctionDecl. We have to
161 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
162 // this, we raid the function's FunctionProtoType for types.
163
Greg Clayton8de27c72010-10-15 22:48:33 +0000164 QualType qual_type (QualType::getFromOpaquePtr(type));
165 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000166
Greg Clayton8de27c72010-10-15 22:48:33 +0000167 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000168 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000169 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000170 unsigned ArgIndex;
171
Greg Clayton8de27c72010-10-15 22:48:33 +0000172 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000173
174 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
175 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000176 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000177
Greg Clayton8de27c72010-10-15 22:48:33 +0000178 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
179 const_cast<DeclContext*>(m_decl_context),
180 SourceLocation(),
181 NULL,
182 arg_qual_type,
183 NULL,
184 SC_Static,
185 SC_Static,
186 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000187 }
188
Greg Clayton8de27c72010-10-15 22:48:33 +0000189 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000190
Greg Clayton8de27c72010-10-15 22:48:33 +0000191 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000192 }
193
Greg Clayton8de27c72010-10-15 22:48:33 +0000194 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000195
Greg Clayton8de27c72010-10-15 22:48:33 +0000196 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000197}
Sean Callanan0fc73582010-07-27 00:55:47 +0000198
199clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
200{
Greg Clayton8de27c72010-10-15 22:48:33 +0000201 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
202 NULL, // argument types
203 0, // number of arguments
204 true, // variadic?
205 0, // type qualifiers
206 false, // has exception specification?
207 false, // has any exception specification?
208 0, // number of exceptions
209 NULL, // exceptions
210 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
211
Sean Callanan0fc73582010-07-27 00:55:47 +0000212 return AddFunDecl(generic_function_type.getAsOpaquePtr());
213}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000214
215clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
216{
Greg Clayton8de27c72010-10-15 22:48:33 +0000217 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000218
Greg Clayton8de27c72010-10-15 22:48:33 +0000219 if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000220 {
221 TagDecl *tag_decl = tag_type->getDecl();
222
Greg Clayton8de27c72010-10-15 22:48:33 +0000223 m_decls.push_back(tag_decl);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000224
225 return tag_decl;
226 }
227 else
228 {
229 return NULL;
230 }
231}