blob: 299524b144f2b92d9c7cf1d3e910ff1791ef5088 [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
73 DeclMap.GetDecls(NSC, Name.getAsString().c_str());
74 return SetExternalVisibleDeclsForName(DC, Name, Decls);
75}
76
77// This is used to support iterating through an entire lexical context,
78// which isn't something the debugger should ever need to do.
79bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, llvm::SmallVectorImpl<Decl*> &Decls) {
80 // true is for error, that's good enough for me
81 return true;
82}
83
84clang::ASTContext *NameSearchContext::GetASTContext() {
85 return &ASTSource.Context;
86}
87
Sean Callanancc074622010-09-14 21:59:34 +000088clang::NamedDecl *NameSearchContext::AddVarDecl(void *type,
89 const char *override_name) {
90 IdentifierInfo *ii = NULL;
91
92 if (override_name)
93 ii = &ASTSource.Context.Idents.get(override_name);
94 else
95 ii = Name.getAsIdentifierInfo();
96
97
Chris Lattner24943d22010-06-08 16:52:24 +000098 clang::NamedDecl *Decl = VarDecl::Create(ASTSource.Context,
99 const_cast<DeclContext*>(DC),
100 SourceLocation(),
Sean Callanancc074622010-09-14 21:59:34 +0000101 ii,
Chris Lattner24943d22010-06-08 16:52:24 +0000102 QualType::getFromOpaquePtr(type),
103 0,
104 VarDecl::Static,
105 VarDecl::Static);
106 Decls.push_back(Decl);
107
108 return Decl;
109}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000110
111clang::NamedDecl *NameSearchContext::AddFunDecl(void *type) {
112 clang::FunctionDecl *Decl = FunctionDecl::Create(ASTSource.Context,
113 const_cast<DeclContext*>(DC),
114 SourceLocation(),
115 Name.getAsIdentifierInfo(),
116 QualType::getFromOpaquePtr(type),
117 NULL,
118 FunctionDecl::Static,
119 FunctionDecl::Static,
120 false,
121 true);
122
Sean Callananb291abe2010-08-12 23:45:38 +0000123 // We have to do more than just synthesize the FunctionDecl. We have to
124 // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
125 // this, we raid the function's FunctionProtoType for types.
126
Sean Callanan8f0dc342010-06-22 23:46:24 +0000127 QualType QT = QualType::getFromOpaquePtr(type);
128 clang::Type *T = QT.getTypePtr();
Sean Callanan3c821cc2010-06-23 18:58:10 +0000129 const FunctionProtoType *FPT = T->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000130
Sean Callanan3c821cc2010-06-23 18:58:10 +0000131 if (FPT)
132 {
Sean Callanan8f0dc342010-06-22 23:46:24 +0000133 unsigned NumArgs = FPT->getNumArgs();
134 unsigned ArgIndex;
135
Sean Callanan3c821cc2010-06-23 18:58:10 +0000136 ParmVarDecl **ParmVarDecls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000137
138 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
139 {
140 QualType ArgQT = FPT->getArgType(ArgIndex);
141
142 ParmVarDecls[ArgIndex] = ParmVarDecl::Create(ASTSource.Context,
143 const_cast<DeclContext*>(DC),
144 SourceLocation(),
145 NULL,
146 ArgQT,
147 NULL,
148 ParmVarDecl::Static,
149 ParmVarDecl::Static,
150 NULL);
151 }
152
153 Decl->setParams(ParmVarDecls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000154
155 delete [] ParmVarDecls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000156 }
157
158 Decls.push_back(Decl);
159
160 return Decl;
161}
Sean Callanan0fc73582010-07-27 00:55:47 +0000162
163clang::NamedDecl *NameSearchContext::AddGenericFunDecl()
164{
165 QualType generic_function_type(ASTSource.Context.getFunctionType(ASTSource.Context.getSizeType(), // result
166 NULL, // argument types
167 0, // number of arguments
168 true, // variadic?
169 0, // type qualifiers
170 false, // has exception specification?
171 false, // has any exception specification?
172 0, // number of exceptions
173 NULL, // exceptions
174 FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention
175
176 return AddFunDecl(generic_function_type.getAsOpaquePtr());
177}
Sean Callanan93a4b1a2010-08-04 01:02:13 +0000178
179clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type)
180{
181 QualType QT = QualType::getFromOpaquePtr(type);
182 clang::Type *T = QT.getTypePtr();
183
184 if (TagType *tag_type = dyn_cast<clang::TagType>(T))
185 {
186 TagDecl *tag_decl = tag_type->getDecl();
187
188 Decls.push_back(tag_decl);
189
190 return tag_decl;
191 }
192 else
193 {
194 return NULL;
195 }
196}