blob: 4f8ef0fa6e0ad2b774e143c86925f41a2d3c70ab [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001/*
2 * ClangASTSource.cpp
3 * lldb
4 *
5 * Created by John McCall on 6/1/10.
6 * Copyright 2010 Apple. All rights reserved.
7 *
8 */
9
Chris Lattner24943d22010-06-08 16:52:24 +000010#include "clang/AST/ASTContext.h"
11#include "lldb/Expression/ClangASTSource.h"
12#include "lldb/Expression/ClangExpression.h"
13#include "lldb/Expression/ClangExpressionDeclMap.h"
14
15using namespace clang;
16using namespace lldb_private;
17
18ClangASTSource::~ClangASTSource() {}
19
20void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) {
21 // Tell Sema to ask us when looking into the translation unit's decl.
22 Context.getTranslationUnitDecl()->setHasExternalVisibleStorage();
23 Context.getTranslationUnitDecl()->setHasExternalLexicalStorage();
24}
25
26// These are only required for AST source that want to lazily load
27// the declarations (or parts thereof) that they return.
28Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; }
29Stmt *ClangASTSource::GetExternalDeclStmt(uint64_t) { return 0; }
30
31// These are also optional, although it might help with ObjC
32// debugging if we have respectable signatures. But a more
33// efficient interface (that didn't require scanning all files
34// for method signatures!) might help.
35Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); }
36uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; }
37
38// The core lookup interface.
39DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName(const DeclContext *DC, DeclarationName Name) {
40 switch (Name.getNameKind()) {
41 // Normal identifiers.
42 case DeclarationName::Identifier:
43 break;
44
45 // Operator names. Not important for now.
46 case DeclarationName::CXXOperatorName:
47 case DeclarationName::CXXLiteralOperatorName:
48 return DeclContext::lookup_result();
49
50 // Using directives found in this context.
51 // Tell Sema we didn't find any or we'll end up getting asked a *lot*.
52 case DeclarationName::CXXUsingDirective:
53 return SetNoExternalVisibleDeclsForName(DC, Name);
54
55 // These aren't looked up like this.
56 case DeclarationName::ObjCZeroArgSelector:
57 case DeclarationName::ObjCOneArgSelector:
58 case DeclarationName::ObjCMultiArgSelector:
59 return DeclContext::lookup_result();
60
61 // These aren't possible in the global context.
62 case DeclarationName::CXXConstructorName:
63 case DeclarationName::CXXDestructorName:
64 case DeclarationName::CXXConversionFunctionName:
65 return DeclContext::lookup_result();
66 }
67
68 llvm::SmallVector<NamedDecl*, 4> Decls;
69
70 NameSearchContext NSC(*this, Decls, Name, DC);
71
72 DeclMap.GetDecls(NSC, Name.getAsString().c_str());
73 return SetExternalVisibleDeclsForName(DC, Name, Decls);
74}
75
76// This is used to support iterating through an entire lexical context,
77// which isn't something the debugger should ever need to do.
78bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, llvm::SmallVectorImpl<Decl*> &Decls) {
79 // true is for error, that's good enough for me
80 return true;
81}
82
83clang::ASTContext *NameSearchContext::GetASTContext() {
84 return &ASTSource.Context;
85}
86
87clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) {
88 clang::NamedDecl *Decl = VarDecl::Create(ASTSource.Context,
89 const_cast<DeclContext*>(DC),
90 SourceLocation(),
91 Name.getAsIdentifierInfo(),
92 QualType::getFromOpaquePtr(type),
93 0,
94 VarDecl::Static,
95 VarDecl::Static);
96 Decls.push_back(Decl);
97
98 return Decl;
99}
Sean Callanan8f0dc342010-06-22 23:46:24 +0000100
101clang::NamedDecl *NameSearchContext::AddFunDecl(void *type) {
102 clang::FunctionDecl *Decl = FunctionDecl::Create(ASTSource.Context,
103 const_cast<DeclContext*>(DC),
104 SourceLocation(),
105 Name.getAsIdentifierInfo(),
106 QualType::getFromOpaquePtr(type),
107 NULL,
108 FunctionDecl::Static,
109 FunctionDecl::Static,
110 false,
111 true);
112
113 QualType QT = QualType::getFromOpaquePtr(type);
114 clang::Type *T = QT.getTypePtr();
Sean Callanan3c821cc2010-06-23 18:58:10 +0000115 const FunctionProtoType *FPT = T->getAs<FunctionProtoType>();
Sean Callanan8f0dc342010-06-22 23:46:24 +0000116
Sean Callanan3c821cc2010-06-23 18:58:10 +0000117 if (FPT)
118 {
Sean Callanan8f0dc342010-06-22 23:46:24 +0000119 unsigned NumArgs = FPT->getNumArgs();
120 unsigned ArgIndex;
121
Sean Callanan3c821cc2010-06-23 18:58:10 +0000122 ParmVarDecl **ParmVarDecls = new ParmVarDecl*[NumArgs];
Sean Callanan8f0dc342010-06-22 23:46:24 +0000123
124 for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex)
125 {
126 QualType ArgQT = FPT->getArgType(ArgIndex);
127
128 ParmVarDecls[ArgIndex] = ParmVarDecl::Create(ASTSource.Context,
129 const_cast<DeclContext*>(DC),
130 SourceLocation(),
131 NULL,
132 ArgQT,
133 NULL,
134 ParmVarDecl::Static,
135 ParmVarDecl::Static,
136 NULL);
137 }
138
139 Decl->setParams(ParmVarDecls, NumArgs);
Sean Callanan3c821cc2010-06-23 18:58:10 +0000140
141 delete [] ParmVarDecls;
Sean Callanan8f0dc342010-06-22 23:46:24 +0000142 }
143
144 Decls.push_back(Decl);
145
146 return Decl;
147}