blob: 34103a6a6539512f58161ea0b22ac003e02bcbbc [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 {
Sean Callananaa301c42010-12-03 01:38:59 +000086 m_decl_map.SetLookupsEnabled ();
Greg Clayton8de27c72010-10-15 22:48:33 +000087 }
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 Callanan1ddd9fe2010-11-30 00:27:43 +0000135
136 assert (type && "Type for variable must be non-NULL!");
Sean Callanancc074622010-09-14 21:59:34 +0000137
Greg Clayton8de27c72010-10-15 22:48:33 +0000138 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
139 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000140 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000141 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000142 QualType::getFromOpaquePtr(type),
143 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000144 SC_Static,
145 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000146 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000147
148 return Decl;
149}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000150
Greg Clayton8de27c72010-10-15 22:48:33 +0000151clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
152 clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
153 const_cast<DeclContext*>(m_decl_context),
154 SourceLocation(),
155 m_decl_name.getAsIdentifierInfo(),
156 QualType::getFromOpaquePtr(type),
157 NULL,
158 SC_Static,
159 SC_Static,
160 false,
161 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000162
Sean Callananb291abe2010-08-12 23:45:38 +0000163 // We have to do more than just synthesize the FunctionDecl. We have to
164 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
165 // this, we raid the function's FunctionProtoType for types.
166
Greg Clayton8de27c72010-10-15 22:48:33 +0000167 QualType qual_type (QualType::getFromOpaquePtr(type));
168 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000169
Greg Clayton8de27c72010-10-15 22:48:33 +0000170 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000171 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000172 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000173 unsigned ArgIndex;
174
Greg Clayton8de27c72010-10-15 22:48:33 +0000175 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000176
177 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
178 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000179 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000180
Greg Clayton8de27c72010-10-15 22:48:33 +0000181 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
182 const_cast<DeclContext*>(m_decl_context),
183 SourceLocation(),
184 NULL,
185 arg_qual_type,
186 NULL,
187 SC_Static,
188 SC_Static,
189 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000190 }
191
Greg Clayton8de27c72010-10-15 22:48:33 +0000192 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000193
Greg Clayton8de27c72010-10-15 22:48:33 +0000194 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000195 }
196
Greg Clayton8de27c72010-10-15 22:48:33 +0000197 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000198
Greg Clayton8de27c72010-10-15 22:48:33 +0000199 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000200}
Sean Callanan0fc73582010-07-27 00:55:47 +0000201
202clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
203{
Greg Clayton8de27c72010-10-15 22:48:33 +0000204 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
205 NULL, // argument types
206 0, // number of arguments
207 true, // variadic?
208 0, // type qualifiers
209 false, // has exception specification?
210 false, // has any exception specification?
211 0, // number of exceptions
212 NULL, // exceptions
213 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
214
Sean Callanan0fc73582010-07-27 00:55:47 +0000215 return AddFunDecl(generic_function_type.getAsOpaquePtr());
216}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000217
218clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
219{
Greg Clayton8de27c72010-10-15 22:48:33 +0000220 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000221
Greg Clayton8de27c72010-10-15 22:48:33 +0000222 if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000223 {
224 TagDecl *tag_decl = tag_type->getDecl();
225
Greg Clayton8de27c72010-10-15 22:48:33 +0000226 m_decls.push_back(tag_decl);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000227
228 return tag_decl;
229 }
Sean Callanan3aa7da52010-12-13 22:46:15 +0000230 else if (ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type))
231 {
232 ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
233
234 m_decls.push_back((NamedDecl*)interface_decl);
235
236 return (NamedDecl*)interface_decl;
237 }
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000238 else
239 {
240 return NULL;
241 }
242}