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. |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 24 | m_ast_context.getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
| 25 | m_ast_context.getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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; } |
Sean Callanan | 8a3b0a8 | 2010-11-18 02:56:27 +0000 | [diff] [blame] | 39 | CXXBaseSpecifier *ClangASTSource::GetExternalCXXBaseSpecifiers(uint64_t Offset) { return NULL; } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | |
| 41 | // The core lookup interface. |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 42 | DeclContext::lookup_result ClangASTSource::FindExternalVisibleDeclsByName |
| 43 | ( |
| 44 | const DeclContext *decl_ctx, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 45 | DeclarationName clang_decl_name |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 46 | ) |
| 47 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 48 | switch (clang_decl_name.getNameKind()) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | // Normal identifiers. |
| 50 | case DeclarationName::Identifier: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 51 | if (clang_decl_name.getAsIdentifierInfo()->getBuiltinID() != 0) |
| 52 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 53 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 54 | |
| 55 | // Operator names. Not important for now. |
| 56 | case DeclarationName::CXXOperatorName: |
| 57 | case DeclarationName::CXXLiteralOperatorName: |
| 58 | return DeclContext::lookup_result(); |
| 59 | |
| 60 | // Using directives found in this context. |
| 61 | // Tell Sema we didn't find any or we'll end up getting asked a *lot*. |
| 62 | case DeclarationName::CXXUsingDirective: |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 63 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | |
| 65 | // These aren't looked up like this. |
| 66 | case DeclarationName::ObjCZeroArgSelector: |
| 67 | case DeclarationName::ObjCOneArgSelector: |
| 68 | case DeclarationName::ObjCMultiArgSelector: |
| 69 | return DeclContext::lookup_result(); |
| 70 | |
| 71 | // These aren't possible in the global context. |
| 72 | case DeclarationName::CXXConstructorName: |
| 73 | case DeclarationName::CXXDestructorName: |
| 74 | case DeclarationName::CXXConversionFunctionName: |
| 75 | return DeclContext::lookup_result(); |
| 76 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 77 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 78 | std::string decl_name (clang_decl_name.getAsString()); |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 79 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 80 | if (!m_decl_map.GetLookupsEnabled()) |
| 81 | { |
| 82 | // Wait until we see a '$' at the start of a name before we start doing |
| 83 | // any lookups so we can avoid lookup up all of the builtin types. |
| 84 | if (!decl_name.empty() && decl_name[0] == '$') |
| 85 | { |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame^] | 86 | m_decl_map.SetLookupsEnabled (); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 87 | } |
| 88 | else |
| 89 | { |
| 90 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 91 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 92 | } |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 93 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 94 | ConstString const_decl_name(decl_name.c_str()); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 95 | |
| 96 | const char *uniqued_const_decl_name = const_decl_name.GetCString(); |
| 97 | if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end()) |
| 98 | { |
| 99 | // We are currently looking up this name... |
| 100 | return DeclContext::lookup_result(); |
| 101 | } |
| 102 | m_active_lookups.insert(uniqued_const_decl_name); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 103 | // static uint32_t g_depth = 0; |
| 104 | // ++g_depth; |
| 105 | // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 106 | llvm::SmallVector<NamedDecl*, 4> name_decls; |
| 107 | NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 108 | m_decl_map.GetDecls(name_search_context, const_decl_name); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 109 | DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls)); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 110 | // --g_depth; |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 111 | m_active_lookups.erase (uniqued_const_decl_name); |
| 112 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 115 | void ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC) |
| 116 | { |
| 117 | return; |
| 118 | } |
| 119 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 120 | // This is used to support iterating through an entire lexical context, |
| 121 | // which isn't something the debugger should ever need to do. |
Sean Callanan | 8950c9a | 2010-10-29 18:38:40 +0000 | [diff] [blame] | 122 | bool ClangASTSource::FindExternalLexicalDecls(const DeclContext *DC, |
| 123 | bool (*isKindWeWant)(Decl::Kind), |
| 124 | llvm::SmallVectorImpl<Decl*> &Decls) { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 125 | // true is for error, that's good enough for me |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | clang::ASTContext *NameSearchContext::GetASTContext() { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 130 | return &m_ast_source.m_ast_context; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Sean Callanan | 3c9c5eb | 2010-09-21 00:44:12 +0000 | [diff] [blame] | 133 | clang::NamedDecl *NameSearchContext::AddVarDecl(void *type) { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 134 | IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); |
Sean Callanan | 1ddd9fe | 2010-11-30 00:27:43 +0000 | [diff] [blame] | 135 | |
| 136 | assert (type && "Type for variable must be non-NULL!"); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 137 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 138 | clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context, |
| 139 | const_cast<DeclContext*>(m_decl_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 140 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 141 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | QualType::getFromOpaquePtr(type), |
| 143 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 144 | SC_Static, |
| 145 | SC_Static); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 146 | m_decls.push_back(Decl); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | |
| 148 | return Decl; |
| 149 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 150 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 151 | clang::NamedDecl *NameSearchContext::AddFunDecl (void *type) { |
| 152 | clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context, |
| 153 | const_cast<DeclContext*>(m_decl_context), |
| 154 | SourceLocation(), |
| 155 | m_decl_name.getAsIdentifierInfo(), |
| 156 | QualType::getFromOpaquePtr(type), |
| 157 | NULL, |
| 158 | SC_Static, |
| 159 | SC_Static, |
| 160 | false, |
| 161 | true); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 162 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 163 | // We have to do more than just synthesize the FunctionDecl. We have to |
| 164 | // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do |
| 165 | // this, we raid the function's FunctionProtoType for types. |
| 166 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 167 | QualType qual_type (QualType::getFromOpaquePtr(type)); |
| 168 | const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 169 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 170 | if (func_proto_type) |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 171 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 172 | unsigned NumArgs = func_proto_type->getNumArgs(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 173 | unsigned ArgIndex; |
| 174 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 175 | ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs]; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 176 | |
| 177 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 178 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 179 | QualType arg_qual_type (func_proto_type->getArgType(ArgIndex)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 180 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 181 | param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context, |
| 182 | const_cast<DeclContext*>(m_decl_context), |
| 183 | SourceLocation(), |
| 184 | NULL, |
| 185 | arg_qual_type, |
| 186 | NULL, |
| 187 | SC_Static, |
| 188 | SC_Static, |
| 189 | NULL); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 192 | func_decl->setParams(param_var_decls, NumArgs); |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 193 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 194 | delete [] param_var_decls; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 197 | m_decls.push_back(func_decl); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 198 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 199 | return func_decl; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 200 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 201 | |
| 202 | clang::NamedDecl *NameSearchContext::AddGenericFunDecl() |
| 203 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 204 | QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.getSizeType(), // result |
| 205 | NULL, // argument types |
| 206 | 0, // number of arguments |
| 207 | true, // variadic? |
| 208 | 0, // type qualifiers |
| 209 | false, // has exception specification? |
| 210 | false, // has any exception specification? |
| 211 | 0, // number of exceptions |
| 212 | NULL, // exceptions |
| 213 | FunctionType::ExtInfo())); // defaults for noreturn, regparm, calling convention |
| 214 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 215 | return AddFunDecl(generic_function_type.getAsOpaquePtr()); |
| 216 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 217 | |
| 218 | clang::NamedDecl *NameSearchContext::AddTypeDecl(void *type) |
| 219 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 220 | QualType qual_type = QualType::getFromOpaquePtr(type); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 221 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 222 | if (TagType *tag_type = dyn_cast<clang::TagType>(qual_type)) |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 223 | { |
| 224 | TagDecl *tag_decl = tag_type->getDecl(); |
| 225 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 226 | m_decls.push_back(tag_decl); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 227 | |
| 228 | return tag_decl; |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | return NULL; |
| 233 | } |
| 234 | } |