Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | /* |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTContext.h" |
| 11 | #include "lldb/Expression/ClangASTSource.h" |
| 12 | #include "lldb/Expression/ClangExpression.h" |
| 13 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 14 | |
| 15 | using namespace clang; |
| 16 | using namespace lldb_private; |
| 17 | |
| 18 | ClangASTSource::~ClangASTSource() {} |
| 19 | |
| 20 | void 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. |
| 28 | Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; } |
| 29 | Stmt *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. |
| 35 | Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); } |
| 36 | uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; } |
| 37 | |
| 38 | // The core lookup interface. |
| 39 | DeclContext::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. |
| 78 | bool 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 | |
| 83 | clang::ASTContext *NameSearchContext::GetASTContext() { |
| 84 | return &ASTSource.Context; |
| 85 | } |
| 86 | |
| 87 | clang::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 Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame^] | 100 | |
| 101 | clang::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(); |
| 115 | |
| 116 | if (T->isFunctionProtoType()) |
| 117 | { |
| 118 | FunctionProtoType *FPT = dyn_cast<FunctionProtoType>(T); |
| 119 | |
| 120 | unsigned NumArgs = FPT->getNumArgs(); |
| 121 | unsigned ArgIndex; |
| 122 | |
| 123 | ParmVarDecl *ParmVarDecls[NumArgs]; |
| 124 | |
| 125 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 126 | { |
| 127 | QualType ArgQT = FPT->getArgType(ArgIndex); |
| 128 | |
| 129 | ParmVarDecls[ArgIndex] = ParmVarDecl::Create(ASTSource.Context, |
| 130 | const_cast<DeclContext*>(DC), |
| 131 | SourceLocation(), |
| 132 | NULL, |
| 133 | ArgQT, |
| 134 | NULL, |
| 135 | ParmVarDecl::Static, |
| 136 | ParmVarDecl::Static, |
| 137 | NULL); |
| 138 | } |
| 139 | |
| 140 | Decl->setParams(ParmVarDecls, NumArgs); |
| 141 | } |
| 142 | |
| 143 | Decls.push_back(Decl); |
| 144 | |
| 145 | return Decl; |
| 146 | } |