blob: e808a04386b6a36689fbef4163ea9818f421ad43 [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; }
Sean Callanan8a3b0a82010-11-18 02:56:27 +000039CXXBaseSpecifier *ClangASTSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) { return NULL; }
Chris Lattner24943d22010-06-08 16:52:24 +000040
41// The core lookup interface.
Greg Claytonf4c7ae02010-10-15 03:36:13 +000042DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName
43(
44 const DeclContext *decl_ctx,
Greg Clayton8de27c72010-10-15 22:48:33 +000045 DeclarationName clang_decl_name
Greg Claytonf4c7ae02010-10-15 03:36:13 +000046)
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 Clayton8de27c72010-10-15 22:48:33 +000078 std::string decl_name (clang_decl_name.getAsString());
Greg Claytonf4c7ae02010-10-15 03:36:13 +000079
Greg Clayton8de27c72010-10-15 22:48:33 +000080 if (!m_decl_map.GetLookupsEnabled())
81 {
82 // Wait until we see a '$' at the start of a name before we start doing
83 // any lookups so we can avoid lookup up all of the builtin types.
84 if (!decl_name.empty() && decl_name[0] == '$')
85 {
86 m_decl_map.SetLookupsEnabled (true);
87 }
88 else
89 {
90 return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name);
91 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000092 }
Greg Clayton8de27c72010-10-15 22:48:33 +000093
Greg Clayton8de27c72010-10-15 22:48:33 +000094 ConstString const_decl_name(decl_name.c_str());
Greg Clayton9ceed1e2010-11-13 04:18:24 +000095
96 const char *uniqued_const_decl_name = const_decl_name.GetCString();
97 if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end())
98 {
99 // We are currently looking up this name...
100 return DeclContext::lookup_result();
101 }
102 m_active_lookups.insert(uniqued_const_decl_name);
Greg Claytona8b278a2010-11-15 01:34:18 +0000103// static uint32_t g_depth = 0;
104// ++g_depth;
105// printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000106 llvm::SmallVector<NamedDecl*, 4> name_decls;
107 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Greg Clayton8de27c72010-10-15 22:48:33 +0000108 m_decl_map.GetDecls(name_search_context, const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000109 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
Greg Claytona8b278a2010-11-15 01:34:18 +0000110// --g_depth;
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000111 m_active_lookups.erase (uniqued_const_decl_name);
112 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000113}
114
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000115void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
116{
117 return;
118}
119
Chris Lattner24943d22010-06-08 16:52:24 +0000120// This is used to support iterating through an entire lexical context,
121// which isn't something the debugger should ever need to do.
Sean Callanan8950c9a2010-10-29 18:38:40 +0000122bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC,
123 bool (*isKindWeWant)(Decl::Kind),
124 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattner24943d22010-06-08 16:52:24 +0000125 // true is for error, that's good enough for me
126 return true;
127}
128
129clang::ASTContext *NameSearchContext::GetASTContext() {
Greg Clayton8de27c72010-10-15 22:48:33 +0000130 return &m_ast_source.m_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000131}
132
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000133clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
Greg Clayton8de27c72010-10-15 22:48:33 +0000134 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanancc074622010-09-14 21:59:34 +0000135
Greg Clayton8de27c72010-10-15 22:48:33 +0000136 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
137 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000138 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000139 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000140 QualType::getFromOpaquePtr(type),
141 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000142 SC_Static,
143 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000144 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000145
146 return Decl;
147}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000148
Greg Clayton8de27c72010-10-15 22:48:33 +0000149clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
150 clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
151 const_cast<DeclContext*>(m_decl_context),
152 SourceLocation(),
153 m_decl_name.getAsIdentifierInfo(),
154 QualType::getFromOpaquePtr(type),
155 NULL,
156 SC_Static,
157 SC_Static,
158 false,
159 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000160
Sean Callananb291abe2010-08-12 23:45:38 +0000161 // We have to do more than just synthesize the FunctionDecl. We have to
162 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
163 // this, we raid the function's FunctionProtoType for types.
164
Greg Clayton8de27c72010-10-15 22:48:33 +0000165 QualType qual_type (QualType::getFromOpaquePtr(type));
166 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000167
Greg Clayton8de27c72010-10-15 22:48:33 +0000168 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000169 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000170 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000171 unsigned ArgIndex;
172
Greg Clayton8de27c72010-10-15 22:48:33 +0000173 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000174
175 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
176 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000177 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000178
Greg Clayton8de27c72010-10-15 22:48:33 +0000179 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
180 const_cast<DeclContext*>(m_decl_context),
181 SourceLocation(),
182 NULL,
183 arg_qual_type,
184 NULL,
185 SC_Static,
186 SC_Static,
187 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000188 }
189
Greg Clayton8de27c72010-10-15 22:48:33 +0000190 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000191
Greg Clayton8de27c72010-10-15 22:48:33 +0000192 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000193 }
194
Greg Clayton8de27c72010-10-15 22:48:33 +0000195 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000196
Greg Clayton8de27c72010-10-15 22:48:33 +0000197 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000198}
Sean Callanan0fc73582010-07-27 00:55:47 +0000199
200clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
201{
Greg Clayton8de27c72010-10-15 22:48:33 +0000202 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
203 NULL, // argument types
204 0, // number of arguments
205 true, // variadic?
206 0, // type qualifiers
207 false, // has exception specification?
208 false, // has any exception specification?
209 0, // number of exceptions
210 NULL, // exceptions
211 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
212
Sean Callanan0fc73582010-07-27 00:55:47 +0000213 return AddFunDecl(generic_function_type.getAsOpaquePtr());
214}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000215
216clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
217{
Greg Clayton8de27c72010-10-15 22:48:33 +0000218 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000219
Greg Clayton8de27c72010-10-15 22:48:33 +0000220 if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000221 {
222 TagDecl *tag_decl = tag_type->getDecl();
223
Greg Clayton8de27c72010-10-15 22:48:33 +0000224 m_decls.push_back(tag_decl);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000225
226 return tag_decl;
227 }
228 else
229 {
230 return NULL;
231 }
232}