blob: f30cb40dc7d5908aeed669fbe6a9c3a18990ee5b [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.
108bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, llvm::SmallVectorImpl<Decl*> &Decls) {
109 // true is for error, that's good enough for me
110 return true;
111}
112
113clang::ASTContext *NameSearchContext::GetASTContext() {
Greg Clayton8de27c72010-10-15 22:48:33 +0000114 return &m_ast_source.m_ast_context;
Chris Lattner24943d22010-06-08 16:52:24 +0000115}
116
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000117clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
Greg Clayton8de27c72010-10-15 22:48:33 +0000118 IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
Sean Callanancc074622010-09-14 21:59:34 +0000119
Greg Clayton8de27c72010-10-15 22:48:33 +0000120 clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context,
121 const_cast<DeclContext*>(m_decl_context),
Chris Lattner24943d22010-06-08 16:52:24 +0000122 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000123 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000124 QualType::getFromOpaquePtr(type),
125 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000126 SC_Static,
127 SC_Static);
Greg Clayton8de27c72010-10-15 22:48:33 +0000128 m_decls.push_back(Decl);
Chris Lattner24943d22010-06-08 16:52:24 +0000129
130 return Decl;
131}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000132
Greg Clayton8de27c72010-10-15 22:48:33 +0000133clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) {
134 clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context,
135 const_cast<DeclContext*>(m_decl_context),
136 SourceLocation(),
137 m_decl_name.getAsIdentifierInfo(),
138 QualType::getFromOpaquePtr(type),
139 NULL,
140 SC_Static,
141 SC_Static,
142 false,
143 true);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000144
Sean Callananb291abe2010-08-12 23:45:38 +0000145 // We have to do more than just synthesize the FunctionDecl. We have to
146 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
147 // this, we raid the function's FunctionProtoType for types.
148
Greg Clayton8de27c72010-10-15 22:48:33 +0000149 QualType qual_type (QualType::getFromOpaquePtr(type));
150 const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000151
Greg Clayton8de27c72010-10-15 22:48:33 +0000152 if (func_proto_type)
Sean Callanan3c821cc2010-06-23 18:58:10 +0000153 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000154 unsigned NumArgs = func_proto_type->getNumArgs();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000155 unsigned ArgIndex;
156
Greg Clayton8de27c72010-10-15 22:48:33 +0000157 ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000158
159 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
160 {
Greg Clayton8de27c72010-10-15 22:48:33 +0000161 QualType arg_qual_type (func_proto_type->getArgType(ArgIndex));
Sean Callanan8f0dc342010-06-22 23:46:24 +0000162
Greg Clayton8de27c72010-10-15 22:48:33 +0000163 param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context,
164 const_cast<DeclContext*>(m_decl_context),
165 SourceLocation(),
166 NULL,
167 arg_qual_type,
168 NULL,
169 SC_Static,
170 SC_Static,
171 NULL);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000172 }
173
Greg Clayton8de27c72010-10-15 22:48:33 +0000174 func_decl->setParams(param_var_decls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000175
Greg Clayton8de27c72010-10-15 22:48:33 +0000176 delete [] param_var_decls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000177 }
178
Greg Clayton8de27c72010-10-15 22:48:33 +0000179 m_decls.push_back(func_decl);
Sean Callanan8f0dc342010-06-22 23:46:24 +0000180
Greg Clayton8de27c72010-10-15 22:48:33 +0000181 return func_decl;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000182}
Sean Callanan0fc73582010-07-27 00:55:47 +0000183
184clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
185{
Greg Clayton8de27c72010-10-15 22:48:33 +0000186 QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result
187 NULL, // argument types
188 0, // number of arguments
189 true, // variadic?
190 0, // type qualifiers
191 false, // has exception specification?
192 false, // has any exception specification?
193 0, // number of exceptions
194 NULL, // exceptions
195 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
196
Sean Callanan0fc73582010-07-27 00:55:47 +0000197 return AddFunDecl(generic_function_type.getAsOpaquePtr());
198}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000199
200clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
201{
Greg Clayton8de27c72010-10-15 22:48:33 +0000202 QualType qual_type = QualType::getFromOpaquePtr(type);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000203
Greg Clayton8de27c72010-10-15 22:48:33 +0000204 if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type))
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000205 {
206 TagDecl *tag_decl = tag_type->getDecl();
207
Greg Clayton8de27c72010-10-15 22:48:33 +0000208 m_decls.push_back(tag_decl);
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000209
210 return tag_decl;
211 }
212 else
213 {
214 return NULL;
215 }
216}