blob: ef2cd9fe60f411500cf0917054a725f6ab0d0e0e [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.
24 Context.getTranslationUnitDecl()->setHasExternalVisibleStorage();
25 Context.getTranslationUnitDecl()->setHasExternalLexicalStorage();
26}
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,
44 DeclarationName decl_name
45)
46{
47 switch (decl_name.getNameKind()) {
Chris Lattner24943d22010-06-08 16:52:24 +000048 // Normal identifiers.
49 case DeclarationName::Identifier:
50 break;
51
52 // Operator names. Not important for now.
53 case DeclarationName::CXXOperatorName:
54 case DeclarationName::CXXLiteralOperatorName:
55 return DeclContext::lookup_result();
56
57 // Using directives found in this context.
58 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
59 case DeclarationName::CXXUsingDirective:
Greg Claytonf4c7ae02010-10-15 03:36:13 +000060 return SetNoExternalVisibleDeclsForName(decl_ctx, decl_name);
Chris Lattner24943d22010-06-08 16:52:24 +000061
62 // These aren't looked up like this.
63 case DeclarationName::ObjCZeroArgSelector:
64 case DeclarationName::ObjCOneArgSelector:
65 case DeclarationName::ObjCMultiArgSelector:
66 return DeclContext::lookup_result();
67
68 // These aren't possible in the global context.
69 case DeclarationName::CXXConstructorName:
70 case DeclarationName::CXXDestructorName:
71 case DeclarationName::CXXConversionFunctionName:
72 return DeclContext::lookup_result();
73 }
Greg Claytonf4c7ae02010-10-15 03:36:13 +000074
75
76 std::string name (decl_name.getAsString());
77 if (0 == name.compare ("__va_list_tag") ||
78 0 == name.compare ("__int128_t") ||
79 0 == name.compare ("__uint128_t") ||
80 0 == name.compare ("SEL") ||
81 0 == name.compare ("id") ||
82 0 == name.compare ("Class") ||
83 0 == name.compare ("nil") ||
84 0 == name.compare ("gp_offset") ||
85 0 == name.compare ("fp_offset") ||
86 0 == name.compare ("overflow_arg_area") ||
87 0 == name.compare ("reg_save_area") ||
88 0 == name.find ("__builtin") )
89 {
90 Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS);
91 if (log)
92 log->Printf("Ignoring built-in in find external declarations for name: '%s'", name.c_str());
93
94 return SetNoExternalVisibleDeclsForName(decl_ctx, decl_name);
95 }
Chris Lattner24943d22010-06-08 16:52:24 +000096
97 llvm::SmallVector<NamedDecl*, 4> Decls;
98
Greg Claytonf4c7ae02010-10-15 03:36:13 +000099 NameSearchContext NSC(*this, Decls, decl_name, decl_ctx);
100 DeclMap.GetDecls(NSC, name.c_str());
101 return SetExternalVisibleDeclsForName(decl_ctx, decl_name, Decls);
Chris Lattner24943d22010-06-08 16:52:24 +0000102}
103
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000104void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC)
105{
106 return;
107}
108
Chris Lattner24943d22010-06-08 16:52:24 +0000109// This is used to support iterating through an entire lexical context,
110// which isn't something the debugger should ever need to do.
111bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, llvm::SmallVectorImpl<Decl*> &Decls) {
112 // true is for error, that's good enough for me
113 return true;
114}
115
116clang::ASTContext *NameSearchContext::GetASTContext() {
117 return &ASTSource.Context;
118}
119
Sean Callanan3c9c5eb2010-09-21 00:44:12 +0000120clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
121 IdentifierInfo *ii = Name.getAsIdentifierInfo();
Sean Callanancc074622010-09-14 21:59:34 +0000122
Chris Lattner24943d22010-06-08 16:52:24 +0000123 clang::NamedDecl *Decl = VarDecl::Create(ASTSource.Context,
124 const_cast<DeclContext*>(DC),
125 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000126 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000127 QualType::getFromOpaquePtr(type),
128 0,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000129 SC_Static,
130 SC_Static);
Chris Lattner24943d22010-06-08 16:52:24 +0000131 Decls.push_back(Decl);
132
133 return Decl;
134}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000135
136clang::NamedDecl *NameSearchContext::AddFunDecl(void *type) {
137 clang::FunctionDecl *Decl = FunctionDecl::Create(ASTSource.Context,
138 const_cast<DeclContext*>(DC),
139 SourceLocation(),
140 Name.getAsIdentifierInfo(),
141 QualType::getFromOpaquePtr(type),
142 NULL,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000143 SC_Static,
144 SC_Static,
Sean Callanan8f0dc342010-06-22 23:46:24 +0000145 false,
146 true);
147
Sean Callananb291abe2010-08-12 23:45:38 +0000148 // We have to do more than just synthesize the FunctionDecl. We have to
149 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
150 // this, we raid the function's FunctionProtoType for types.
151
Sean Callanan8f0dc342010-06-22 23:46:24 +0000152 QualType QT = QualType::getFromOpaquePtr(type);
153 clang::Type *T = QT.getTypePtr();
Sean Callanan3c821cc2010-06-23 18:58:10 +0000154 const FunctionProtoType *FPT = T->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000155
Sean Callanan3c821cc2010-06-23 18:58:10 +0000156 if (FPT)
157 {
Sean Callanan8f0dc342010-06-22 23:46:24 +0000158 unsigned NumArgs = FPT->getNumArgs();
159 unsigned ArgIndex;
160
Sean Callanan3c821cc2010-06-23 18:58:10 +0000161 ParmVarDecl **ParmVarDecls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000162
163 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
164 {
165 QualType ArgQT = FPT->getArgType(ArgIndex);
166
167 ParmVarDecls[ArgIndex] = ParmVarDecl::Create(ASTSource.Context,
168 const_cast<DeclContext*>(DC),
169 SourceLocation(),
170 NULL,
171 ArgQT,
172 NULL,
Sean Callanan47a5c4c2010-09-23 03:01:22 +0000173 SC_Static,
174 SC_Static,
Sean Callanan8f0dc342010-06-22 23:46:24 +0000175 NULL);
176 }
177
178 Decl->setParams(ParmVarDecls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000179
180 delete [] ParmVarDecls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000181 }
182
183 Decls.push_back(Decl);
184
185 return Decl;
186}
Sean Callanan0fc73582010-07-27 00:55:47 +0000187
188clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
189{
190 QualType generic_function_type(ASTSource.Context.getFunctionType(ASTSource.Context.getSizeType(), // result
191 NULL, // argument types
192 0, // number of arguments
193 true, // variadic?
194 0, // type qualifiers
195 false, // has exception specification?
196 false, // has any exception specification?
197 0, // number of exceptions
198 NULL, // exceptions
199 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
200
201 return AddFunDecl(generic_function_type.getAsOpaquePtr());
202}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000203
204clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
205{
206 QualType QT = QualType::getFromOpaquePtr(type);
207 clang::Type *T = QT.getTypePtr();
208
209 if (TagType *tag_type = dyn_cast<clang::TagType>(T))
210 {
211 TagDecl *tag_decl = tag_type->getDecl();
212
213 Decls.push_back(tag_decl);
214
215 return tag_decl;
216 }
217 else
218 {
219 return NULL;
220 }
221}