Greg Clayton | 1e591ce | 2010-07-16 18:28:27 +0000 | [diff] [blame] | 1 | //===-- 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 10 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTContext.h" |
| 12 | #include "lldb/Expression/ClangASTSource.h" |
| 13 | #include "lldb/Expression/ClangExpression.h" |
| 14 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 15 | |
| 16 | using namespace clang; |
| 17 | using namespace lldb_private; |
| 18 | |
| 19 | ClangASTSource::~ClangASTSource() {} |
| 20 | |
| 21 | void 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. |
| 29 | Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; } |
| 30 | Stmt *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. |
| 36 | Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); } |
| 37 | uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; } |
| 38 | |
| 39 | // The core lookup interface. |
| 40 | DeclContext::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 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame^] | 77 | void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC) |
| 78 | { |
| 79 | return; |
| 80 | } |
| 81 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | // This is used to support iterating through an entire lexical context, |
| 83 | // which isn't something the debugger should ever need to do. |
| 84 | bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, llvm::SmallVectorImpl<Decl*> &Decls) { |
| 85 | // true is for error, that's good enough for me |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | clang::ASTContext *NameSearchContext::GetASTContext() { |
| 90 | return &ASTSource.Context; |
| 91 | } |
| 92 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 93 | clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) { |
| 94 | IdentifierInfo *ii = Name.getAsIdentifierInfo(); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | clang::NamedDecl *Decl = VarDecl::Create(ASTSource.Context, |
| 97 | const_cast<DeclContext*>(DC), |
| 98 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 99 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | QualType::getFromOpaquePtr(type), |
| 101 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame^] | 102 | SC_Static, |
| 103 | SC_Static); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | Decls.push_back(Decl); |
| 105 | |
| 106 | return Decl; |
| 107 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 108 | |
| 109 | clang::NamedDecl *NameSearchContext::AddFunDecl(void *type) { |
| 110 | clang::FunctionDecl *Decl = FunctionDecl::Create(ASTSource.Context, |
| 111 | const_cast<DeclContext*>(DC), |
| 112 | SourceLocation(), |
| 113 | Name.getAsIdentifierInfo(), |
| 114 | QualType::getFromOpaquePtr(type), |
| 115 | NULL, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame^] | 116 | SC_Static, |
| 117 | SC_Static, |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 118 | false, |
| 119 | true); |
| 120 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 121 | // We have to do more than just synthesize the FunctionDecl. We have to |
| 122 | // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do |
| 123 | // this, we raid the function's FunctionProtoType for types. |
| 124 | |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 125 | QualType QT = QualType::getFromOpaquePtr(type); |
| 126 | clang::Type *T = QT.getTypePtr(); |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 127 | const FunctionProtoType *FPT = T->getAs<FunctionProtoType>(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 128 | |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 129 | if (FPT) |
| 130 | { |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 131 | unsigned NumArgs = FPT->getNumArgs(); |
| 132 | unsigned ArgIndex; |
| 133 | |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 134 | ParmVarDecl **ParmVarDecls = new ParmVarDecl*[NumArgs]; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 135 | |
| 136 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 137 | { |
| 138 | QualType ArgQT = FPT->getArgType(ArgIndex); |
| 139 | |
| 140 | ParmVarDecls[ArgIndex] = ParmVarDecl::Create(ASTSource.Context, |
| 141 | const_cast<DeclContext*>(DC), |
| 142 | SourceLocation(), |
| 143 | NULL, |
| 144 | ArgQT, |
| 145 | NULL, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame^] | 146 | SC_Static, |
| 147 | SC_Static, |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 148 | NULL); |
| 149 | } |
| 150 | |
| 151 | Decl->setParams(ParmVarDecls, NumArgs); |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 152 | |
| 153 | delete [] ParmVarDecls; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | Decls.push_back(Decl); |
| 157 | |
| 158 | return Decl; |
| 159 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 160 | |
| 161 | clang::NamedDecl *NameSearchContext::AddGenericFunDecl() |
| 162 | { |
| 163 | QualType generic_function_type(ASTSource.Context.getFunctionType(ASTSource.Context.getSizeType(), // result |
| 164 | NULL, // argument types |
| 165 | 0, // number of arguments |
| 166 | true, // variadic? |
| 167 | 0, // type qualifiers |
| 168 | false, // has exception specification? |
| 169 | false, // has any exception specification? |
| 170 | 0, // number of exceptions |
| 171 | NULL, // exceptions |
| 172 | FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention |
| 173 | |
| 174 | return AddFunDecl(generic_function_type.getAsOpaquePtr()); |
| 175 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 176 | |
| 177 | clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type) |
| 178 | { |
| 179 | QualType QT = QualType::getFromOpaquePtr(type); |
| 180 | clang::Type *T = QT.getTypePtr(); |
| 181 | |
| 182 | if (TagType *tag_type = dyn_cast<clang::TagType>(T)) |
| 183 | { |
| 184 | TagDecl *tag_decl = tag_type->getDecl(); |
| 185 | |
| 186 | Decls.push_back(tag_decl); |
| 187 | |
| 188 | return tag_decl; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | return NULL; |
| 193 | } |
| 194 | } |