blob: 7d3a8c64aa905dcb9295cbf7c503b6a28fdc8f80 [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
93 llvm::SmallVector<NamedDecl*, 4> name_decls;
Chris Lattner24943d22010-06-08 16:52:24 +000094
Greg Clayton8de27c72010-10-15 22:48:33 +000095 NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx);
96 ConstString const_decl_name(decl_name.c_str());
97 m_decl_map.GetDecls(name_search_context, const_decl_name);
98 return SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls);
Chris Lattner24943d22010-06-08 16:52:24 +000099}
100
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000101void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
102{
103 return;
104}
105
Chris Lattner24943d22010-06-08 16:52:24 +0000106// This is used to support iterating through an entire lexical context,
107// which isn't something the debugger should ever need to do.
Sean Callanan8950c9a2010-10-29 18:38:40 +0000108bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC,
109 bool (*isKindWeWant)(Decl::Kind),
110 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattner24943d22010-06-08 16:52:24 +0000111 // true is for error, that's good enough for me
112 return true;
113}
114
115clang::ASTContext *NameSearchContext::GetASTContext() {
Greg Clayton8de27c72010-10-15 22:48:33 +0000116 return &m_ast_source.m_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000117}
118
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000119clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
Greg Clayton8de27c72010-10-15 22:48:33 +0000120 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanancc074622010-09-14 21:59:34 +0000121
Greg Clayton8de27c72010-10-15 22:48:33 +0000122 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
123 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000124 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000125 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000126 QualType::getFromOpaquePtr(type),
127 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000128 SC_Static,
129 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000130 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000131
132 return Decl;
133}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000134
Greg Clayton8de27c72010-10-15 22:48:33 +0000135clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
136 clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
137 const_cast<DeclContext*>(m_decl_context),
138 SourceLocation(),
139 m_decl_name.getAsIdentifierInfo(),
140 QualType::getFromOpaquePtr(type),
141 NULL,
142 SC_Static,
143 SC_Static,
144 false,
145 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000146
Sean Callananb291abe2010-08-12 23:45:38 +0000147 // We have to do more than just synthesize the FunctionDecl. We have to
148 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
149 // this, we raid the function's FunctionProtoType for types.
150
Greg Clayton8de27c72010-10-15 22:48:33 +0000151 QualType qual_type (QualType::getFromOpaquePtr(type));
152 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000153
Greg Clayton8de27c72010-10-15 22:48:33 +0000154 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000155 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000156 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000157 unsigned ArgIndex;
158
Greg Clayton8de27c72010-10-15 22:48:33 +0000159 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000160
161 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
162 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000163 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000164
Greg Clayton8de27c72010-10-15 22:48:33 +0000165 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
166 const_cast<DeclContext*>(m_decl_context),
167 SourceLocation(),
168 NULL,
169 arg_qual_type,
170 NULL,
171 SC_Static,
172 SC_Static,
173 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000174 }
175
Greg Clayton8de27c72010-10-15 22:48:33 +0000176 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000177
Greg Clayton8de27c72010-10-15 22:48:33 +0000178 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000179 }
180
Greg Clayton8de27c72010-10-15 22:48:33 +0000181 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000182
Greg Clayton8de27c72010-10-15 22:48:33 +0000183 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000184}
Sean Callanan0fc73582010-07-27 00:55:47 +0000185
186clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
187{
Greg Clayton8de27c72010-10-15 22:48:33 +0000188 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
189 NULL, // argument types
190 0, // number of arguments
191 true, // variadic?
192 0, // type qualifiers
193 false, // has exception specification?
194 false, // has any exception specification?
195 0, // number of exceptions
196 NULL, // exceptions
197 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
198
Sean Callanan0fc73582010-07-27 00:55:47 +0000199 return AddFunDecl(generic_function_type.getAsOpaquePtr());
200}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000201
202clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
203{
Greg Clayton8de27c72010-10-15 22:48:33 +0000204 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000205
Greg Clayton8de27c72010-10-15 22:48:33 +0000206 if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000207 {
208 TagDecl *tag_decl = tag_type->getDecl();
209
Greg Clayton8de27c72010-10-15 22:48:33 +0000210 m_decls.push_back(tag_decl);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000211
212 return tag_decl;
213 }
214 else
215 {
216 return NULL;
217 }
218}