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" |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame^] | 12 | #include "lldb/Core/Log.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Expression/ClangASTSource.h" |
| 14 | #include "lldb/Expression/ClangExpression.h" |
| 15 | #include "lldb/Expression/ClangExpressionDeclMap.h" |
| 16 | |
| 17 | using namespace clang; |
| 18 | using namespace lldb_private; |
| 19 | |
| 20 | ClangASTSource::~ClangASTSource() {} |
| 21 | |
| 22 | void 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. |
| 30 | Decl *ClangASTSource::GetExternalDecl(uint32_t) { return 0; } |
| 31 | Stmt *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. |
| 37 | Selector ClangASTSource::GetExternalSelector(uint32_t) { return Selector(); } |
| 38 | uint32_t ClangASTSource::GetNumExternalSelectors() { return 0; } |
| 39 | |
| 40 | // The core lookup interface. |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame^] | 41 | DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName |
| 42 | ( |
| 43 | const DeclContext *decl_ctx, |
| 44 | DeclarationName decl_name |
| 45 | ) |
| 46 | { |
| 47 | switch (decl_name.getNameKind()) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | // 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 Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame^] | 60 | return SetNoExternalVisibleDeclsForName(decl_ctx, decl_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | |
| 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 Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame^] | 74 | |
| 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 Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | |
| 97 | llvm::SmallVector<NamedDecl*, 4> Decls; |
| 98 | |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame^] | 99 | NameSearchContext NSC(*this, Decls, decl_name, decl_ctx); |
| 100 | DeclMap.GetDecls(NSC, name.c_str()); |
| 101 | return SetExternalVisibleDeclsForName(decl_ctx, decl_name, Decls); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 104 | void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC) |
| 105 | { |
| 106 | return; |
| 107 | } |
| 108 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | // This is used to support iterating through an entire lexical context, |
| 110 | // which isn't something the debugger should ever need to do. |
| 111 | bool 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 | |
| 116 | clang::ASTContext *NameSearchContext::GetASTContext() { |
| 117 | return &ASTSource.Context; |
| 118 | } |
| 119 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 120 | clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) { |
| 121 | IdentifierInfo *ii = Name.getAsIdentifierInfo(); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | clang::NamedDecl *Decl = VarDecl::Create(ASTSource.Context, |
| 124 | const_cast<DeclContext*>(DC), |
| 125 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 126 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | QualType::getFromOpaquePtr(type), |
| 128 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 129 | SC_Static, |
| 130 | SC_Static); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | Decls.push_back(Decl); |
| 132 | |
| 133 | return Decl; |
| 134 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 135 | |
| 136 | clang::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 Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 143 | SC_Static, |
| 144 | SC_Static, |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 145 | false, |
| 146 | true); |
| 147 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 148 | // 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 Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 152 | QualType QT = QualType::getFromOpaquePtr(type); |
| 153 | clang::Type *T = QT.getTypePtr(); |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 154 | const FunctionProtoType *FPT = T->getAs<FunctionProtoType>(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 155 | |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 156 | if (FPT) |
| 157 | { |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 158 | unsigned NumArgs = FPT->getNumArgs(); |
| 159 | unsigned ArgIndex; |
| 160 | |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 161 | ParmVarDecl **ParmVarDecls = new ParmVarDecl*[NumArgs]; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 162 | |
| 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 Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 173 | SC_Static, |
| 174 | SC_Static, |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 175 | NULL); |
| 176 | } |
| 177 | |
| 178 | Decl->setParams(ParmVarDecls, NumArgs); |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 179 | |
| 180 | delete [] ParmVarDecls; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | Decls.push_back(Decl); |
| 184 | |
| 185 | return Decl; |
| 186 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 187 | |
| 188 | clang::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 Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 203 | |
| 204 | clang::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 | } |