blob: aa69abf1faf66ac80f6da2a218bbe4812de76e49 [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);
102 llvm::SmallVector<NamedDecl*, 4> name_decls;
103 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
Greg Clayton8de27c72010-10-15 22:48:33 +0000104 m_decl_map.GetDecls(name_search_context, const_decl_name);
Greg Clayton9ceed1e2010-11-13 04:18:24 +0000105 DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls));
106 m_active_lookups.erase (uniqued_const_decl_name);
107 return result;
Chris Lattner24943d22010-06-08 16:52:24 +0000108}
109
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000110void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
111{
112 return;
113}
114
Chris Lattner24943d22010-06-08 16:52:24 +0000115// This is used to support iterating through an entire lexical context,
116// which isn't something the debugger should ever need to do.
Sean Callanan8950c9a2010-10-29 18:38:40 +0000117bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC,
118 bool (*isKindWeWant)(Decl::Kind),
119 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattner24943d22010-06-08 16:52:24 +0000120 // true is for error, that's good enough for me
121 return true;
122}
123
124clang::ASTContext *NameSearchContext::GetASTContext() {
Greg Clayton8de27c72010-10-15 22:48:33 +0000125 return &m_ast_source.m_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000126}
127
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000128clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
Greg Clayton8de27c72010-10-15 22:48:33 +0000129 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanancc074622010-09-14 21:59:34 +0000130
Greg Clayton8de27c72010-10-15 22:48:33 +0000131 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
132 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000133 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000134 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000135 QualType::getFromOpaquePtr(type),
136 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000137 SC_Static,
138 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000139 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000140
141 return Decl;
142}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000143
Greg Clayton8de27c72010-10-15 22:48:33 +0000144clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
145 clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
146 const_cast<DeclContext*>(m_decl_context),
147 SourceLocation(),
148 m_decl_name.getAsIdentifierInfo(),
149 QualType::getFromOpaquePtr(type),
150 NULL,
151 SC_Static,
152 SC_Static,
153 false,
154 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000155
Sean Callananb291abe2010-08-12 23:45:38 +0000156 // We have to do more than just synthesize the FunctionDecl. We have to
157 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
158 // this, we raid the function's FunctionProtoType for types.
159
Greg Clayton8de27c72010-10-15 22:48:33 +0000160 QualType qual_type (QualType::getFromOpaquePtr(type));
161 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000162
Greg Clayton8de27c72010-10-15 22:48:33 +0000163 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000164 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000165 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000166 unsigned ArgIndex;
167
Greg Clayton8de27c72010-10-15 22:48:33 +0000168 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000169
170 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
171 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000172 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000173
Greg Clayton8de27c72010-10-15 22:48:33 +0000174 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
175 const_cast<DeclContext*>(m_decl_context),
176 SourceLocation(),
177 NULL,
178 arg_qual_type,
179 NULL,
180 SC_Static,
181 SC_Static,
182 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000183 }
184
Greg Clayton8de27c72010-10-15 22:48:33 +0000185 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000186
Greg Clayton8de27c72010-10-15 22:48:33 +0000187 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000188 }
189
Greg Clayton8de27c72010-10-15 22:48:33 +0000190 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000191
Greg Clayton8de27c72010-10-15 22:48:33 +0000192 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000193}
Sean Callanan0fc73582010-07-27 00:55:47 +0000194
195clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
196{
Greg Clayton8de27c72010-10-15 22:48:33 +0000197 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
198 NULL, // argument types
199 0, // number of arguments
200 true, // variadic?
201 0, // type qualifiers
202 false, // has exception specification?
203 false, // has any exception specification?
204 0, // number of exceptions
205 NULL, // exceptions
206 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
207
Sean Callanan0fc73582010-07-27 00:55:47 +0000208 return AddFunDecl(generic_function_type.getAsOpaquePtr());
209}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000210
211clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
212{
Greg Clayton8de27c72010-10-15 22:48:33 +0000213 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000214
Greg Clayton8de27c72010-10-15 22:48:33 +0000215 if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000216 {
217 TagDecl *tag_decl = tag_type->getDecl();
218
Greg Clayton8de27c72010-10-15 22:48:33 +0000219 m_decls.push_back(tag_decl);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000220
221 return tag_decl;
222 }
223 else
224 {
225 return NULL;
226 }
227}