blob: 0808b4ccde5f548bf1a642f56383a2b6b9c617d9 [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"
12#include "lldb/Expression/ClangASTSource.h"
13#include "lldb/Expression/ClangExpression.h"
14#include "lldb/Expression/ClangExpressionDeclMap.h"
15
16using namespace clang;
17using namespace lldb_private;
18
19ClangASTSource::~ClangASTSource() {}
20
21void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
22 // Tell Sema to ask us when looking into the translation unit's decl.
23 Context.getTranslationUnitDecl()->setHasExternalVisibleStorage();
24 Context.getTranslationUnitDecl()->setHasExternalLexicalStorage();
25}
26
27// These are only required for AST source that want to lazily load
28// the declarations (or parts thereof) that they return.
29Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; }
30Stmt *ClangASTSource::GetExternalDeclStmt(uint64_t) { return 0; }
31
32// These are also optional, although it might help with ObjC
33// debugging if we have respectable signatures. But a more
34// efficient interface (that didn't require scanning all files
35// for method signatures!) might help.
36Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); }
37uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; }
38
39// The core lookup interface.
40DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) {
41 switch (Name.getNameKind()) {
42 // Normal identifiers.
43 case DeclarationName::Identifier:
44 break;
45
46 // Operator names. Not important for now.
47 case DeclarationName::CXXOperatorName:
48 case DeclarationName::CXXLiteralOperatorName:
49 return DeclContext::lookup_result();
50
51 // Using directives found in this context.
52 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
53 case DeclarationName::CXXUsingDirective:
54 return SetNoExternalVisibleDeclsForName(DC, Name);
55
56 // These aren't looked up like this.
57 case DeclarationName::ObjCZeroArgSelector:
58 case DeclarationName::ObjCOneArgSelector:
59 case DeclarationName::ObjCMultiArgSelector:
60 return DeclContext::lookup_result();
61
62 // These aren't possible in the global context.
63 case DeclarationName::CXXConstructorName:
64 case DeclarationName::CXXDestructorName:
65 case DeclarationName::CXXConversionFunctionName:
66 return DeclContext::lookup_result();
67 }
68
69 llvm::SmallVector<NamedDecl*, 4> Decls;
70
71 NameSearchContext NSC(*this, Decls, Name, DC);
72
Greg Claytonfb7c51c2010-10-13 03:15:28 +000073 std::string name (Name.getAsString());
74 // TODO: Figure out what to do here, after recent changes to the DWARF
75 // parser where more types are now in type by name index, we were sometimes
76 // finding our own version of a builtin? Skip it for now until we figure out
77 // how to get around this properly.
78 if (name.compare("__va_list_tag") != 0)
79 DeclMap.GetDecls(NSC, name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +000080 return SetExternalVisibleDeclsForName(DC, Name, Decls);
81}
82
Sean Callanan47a5c4c2010-09-23 03:01:22 +000083void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
84{
85 return;
86}
87
Chris Lattner24943d22010-06-08 16:52:24 +000088// This is used to support iterating through an entire lexical context,
89// which isn't something the debugger should ever need to do.
90bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, llvm::SmallVectorImpl<Decl*> &Decls) {
91 // true is for error, that's good enough for me
92 return true;
93}
94
95clang::ASTContext *NameSearchContext::GetASTContext() {
96 return &ASTSource.Context;
97}
98
Sean Callanan3c9c5eb2010-09-21 00:44:12 +000099clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
100 IdentifierInfo *ii = Name.getAsIdentifierInfo();
Sean Callanancc074622010-09-14 21:59:34 +0000101
Chris Lattner24943d22010-06-08 16:52:24 +0000102 clang::NamedDecl *Decl = VarDecl::Create(ASTSource.Context,
103 const_cast<DeclContext*>(DC),
104 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000105 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000106 QualType::getFromOpaquePtr(type),
107 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000108 SC_Static,
109 SC_Static);
Chris Lattner24943d22010-06-08 16:52:24 +0000110 Decls.push_back(Decl);
111
112 return Decl;
113}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000114
115clang::NamedDecl *NameSearchContext::AddFunDecl(void *type) {
116 clang::FunctionDecl *Decl = FunctionDecl::Create(ASTSource.Context,
117 const_cast<DeclContext*>(DC),
118 SourceLocation(),
119 Name.getAsIdentifierInfo(),
120 QualType::getFromOpaquePtr(type),
121 NULL,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000122 SC_Static,
123 SC_Static,
Sean Callanan8f0dc342010-06-22 23:46:24 +0000124 false,
125 true);
126
Sean Callananb291abe2010-08-12 23:45:38 +0000127 // We have to do more than just synthesize the FunctionDecl. We have to
128 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
129 // this, we raid the function's FunctionProtoType for types.
130
Sean Callanan8f0dc342010-06-22 23:46:24 +0000131 QualType QT = QualType::getFromOpaquePtr(type);
132 clang::Type *T = QT.getTypePtr();
Sean Callanan3c821cc2010-06-23 18:58:10 +0000133 const FunctionProtoType *FPT = T->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000134
Sean Callanan3c821cc2010-06-23 18:58:10 +0000135 if (FPT)
136 {
Sean Callanan8f0dc342010-06-22 23:46:24 +0000137 unsigned NumArgs = FPT->getNumArgs();
138 unsigned ArgIndex;
139
Sean Callanan3c821cc2010-06-23 18:58:10 +0000140 ParmVarDecl **ParmVarDecls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000141
142 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
143 {
144 QualType ArgQT = FPT->getArgType(ArgIndex);
145
146 ParmVarDecls[ArgIndex] = ParmVarDecl::Create(ASTSource.Context,
147 const_cast<DeclContext*>(DC),
148 SourceLocation(),
149 NULL,
150 ArgQT,
151 NULL,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000152 SC_Static,
153 SC_Static,
Sean Callanan8f0dc342010-06-22 23:46:24 +0000154 NULL);
155 }
156
157 Decl->setParams(ParmVarDecls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000158
159 delete [] ParmVarDecls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000160 }
161
162 Decls.push_back(Decl);
163
164 return Decl;
165}
Sean Callanan0fc73582010-07-27 00:55:47 +0000166
167clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
168{
169 QualType generic_function_type(ASTSource.Context.getFunctionType(ASTSource.Context.getSizeType(), // result
170 NULL, // argument types
171 0, // number of arguments
172 true, // variadic?
173 0, // type qualifiers
174 false, // has exception specification?
175 false, // has any exception specification?
176 0, // number of exceptions
177 NULL, // exceptions
178 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
179
180 return AddFunDecl(generic_function_type.getAsOpaquePtr());
181}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000182
183clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
184{
185 QualType QT = QualType::getFromOpaquePtr(type);
186 clang::Type *T = QT.getTypePtr();
187
188 if (TagType *tag_type = dyn_cast<clang::TagType>(T))
189 {
190 TagDecl *tag_decl = tag_type->getDecl();
191
192 Decls.push_back(tag_decl);
193
194 return tag_decl;
195 }
196 else
197 {
198 return NULL;
199 }
200}