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 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 20 | ClangASTSource::~ClangASTSource() |
| 21 | { |
| 22 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 23 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 24 | void |
| 25 | ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) |
| 26 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | // 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] | 28 | m_ast_context.getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
| 29 | m_ast_context.getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | // The core lookup interface. |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 33 | DeclContext::lookup_result |
| 34 | ClangASTSource::FindExternalVisibleDeclsByName |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 35 | ( |
| 36 | const DeclContext *decl_ctx, |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 37 | DeclarationName clang_decl_name |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 38 | ) |
| 39 | { |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 40 | if (m_decl_map.GetImportInProgress()) |
| 41 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 42 | |
| 43 | std::string decl_name (clang_decl_name.getAsString()); |
| 44 | |
| 45 | // if (m_decl_map.DoingASTImport ()) |
| 46 | // return DeclContext::lookup_result(); |
| 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 | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 78 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 79 | if (!m_decl_map.GetLookupsEnabled()) |
| 80 | { |
| 81 | // Wait until we see a '$' at the start of a name before we start doing |
| 82 | // any lookups so we can avoid lookup up all of the builtin types. |
| 83 | if (!decl_name.empty() && decl_name[0] == '$') |
| 84 | { |
Sean Callanan | aa301c4 | 2010-12-03 01:38:59 +0000 | [diff] [blame] | 85 | m_decl_map.SetLookupsEnabled (); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 86 | } |
| 87 | else |
| 88 | { |
| 89 | return SetNoExternalVisibleDeclsForName(decl_ctx, clang_decl_name); |
| 90 | } |
Greg Clayton | f4c7ae0 | 2010-10-15 03:36:13 +0000 | [diff] [blame] | 91 | } |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 92 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 93 | ConstString const_decl_name(decl_name.c_str()); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 94 | |
| 95 | const char *uniqued_const_decl_name = const_decl_name.GetCString(); |
| 96 | if (m_active_lookups.find (uniqued_const_decl_name) != m_active_lookups.end()) |
| 97 | { |
| 98 | // We are currently looking up this name... |
| 99 | return DeclContext::lookup_result(); |
| 100 | } |
| 101 | m_active_lookups.insert(uniqued_const_decl_name); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 102 | // static uint32_t g_depth = 0; |
| 103 | // ++g_depth; |
| 104 | // printf("[%5u] FindExternalVisibleDeclsByName() \"%s\"\n", g_depth, uniqued_const_decl_name); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 105 | llvm::SmallVector<NamedDecl*, 4> name_decls; |
| 106 | NameSearchContext name_search_context(*this, name_decls, clang_decl_name, decl_ctx); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 107 | m_decl_map.GetDecls(name_search_context, const_decl_name); |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 108 | DeclContext::lookup_result result (SetExternalVisibleDeclsForName (decl_ctx, clang_decl_name, name_decls)); |
Greg Clayton | a8b278a | 2010-11-15 01:34:18 +0000 | [diff] [blame] | 109 | // --g_depth; |
Greg Clayton | 9ceed1e | 2010-11-13 04:18:24 +0000 | [diff] [blame] | 110 | m_active_lookups.erase (uniqued_const_decl_name); |
| 111 | return result; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 114 | void |
| 115 | ClangASTSource::CompleteType (TagDecl *tag_decl) |
| 116 | { |
| 117 | puts(__PRETTY_FUNCTION__); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | ClangASTSource::CompleteType (ObjCInterfaceDecl *objc_decl) |
| 122 | { |
| 123 | puts(__PRETTY_FUNCTION__); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | ClangASTSource::MaterializeVisibleDecls(const DeclContext *DC) |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 128 | { |
| 129 | return; |
| 130 | } |
| 131 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | // This is used to support iterating through an entire lexical context, |
| 133 | // which isn't something the debugger should ever need to do. |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 134 | bool |
| 135 | ClangASTSource::FindExternalLexicalDecls |
| 136 | ( |
| 137 | const DeclContext *DC, |
| 138 | bool (*isKindWeWant)(Decl::Kind), |
| 139 | llvm::SmallVectorImpl<Decl*> &Decls |
| 140 | ) |
| 141 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | // true is for error, that's good enough for me |
| 143 | return true; |
| 144 | } |
| 145 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 146 | clang::ASTContext * |
| 147 | NameSearchContext::GetASTContext() |
| 148 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 149 | return &m_ast_source.m_ast_context; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 152 | clang::NamedDecl * |
| 153 | NameSearchContext::AddVarDecl(void *type) |
| 154 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 155 | IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo(); |
Sean Callanan | 1ddd9fe | 2010-11-30 00:27:43 +0000 | [diff] [blame] | 156 | |
| 157 | assert (type && "Type for variable must be non-NULL!"); |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 158 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 159 | clang::NamedDecl *Decl = VarDecl::Create(m_ast_source.m_ast_context, |
| 160 | const_cast<DeclContext*>(m_decl_context), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 161 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 162 | SourceLocation(), |
Sean Callanan | cc07462 | 2010-09-14 21:59:34 +0000 | [diff] [blame] | 163 | ii, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | QualType::getFromOpaquePtr(type), |
| 165 | 0, |
Sean Callanan | 47a5c4c | 2010-09-23 03:01:22 +0000 | [diff] [blame] | 166 | SC_Static, |
| 167 | SC_Static); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 168 | m_decls.push_back(Decl); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 169 | |
| 170 | return Decl; |
| 171 | } |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 172 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 173 | clang::NamedDecl * |
| 174 | NameSearchContext::AddFunDecl (void *type) |
| 175 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 176 | clang::FunctionDecl *func_decl = FunctionDecl::Create (m_ast_source.m_ast_context, |
| 177 | const_cast<DeclContext*>(m_decl_context), |
| 178 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 179 | SourceLocation(), |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 180 | m_decl_name.getAsIdentifierInfo(), |
| 181 | QualType::getFromOpaquePtr(type), |
| 182 | NULL, |
| 183 | SC_Static, |
| 184 | SC_Static, |
| 185 | false, |
| 186 | true); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 187 | |
Sean Callanan | b291abe | 2010-08-12 23:45:38 +0000 | [diff] [blame] | 188 | // We have to do more than just synthesize the FunctionDecl. We have to |
| 189 | // synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do |
| 190 | // this, we raid the function's FunctionProtoType for types. |
| 191 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 192 | QualType qual_type (QualType::getFromOpaquePtr(type)); |
| 193 | const FunctionProtoType *func_proto_type = qual_type->getAs<FunctionProtoType>(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 194 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 195 | if (func_proto_type) |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 196 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 197 | unsigned NumArgs = func_proto_type->getNumArgs(); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 198 | unsigned ArgIndex; |
| 199 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 200 | ParmVarDecl **param_var_decls = new ParmVarDecl*[NumArgs]; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 201 | |
| 202 | for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) |
| 203 | { |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 204 | QualType arg_qual_type (func_proto_type->getArgType(ArgIndex)); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 205 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 206 | param_var_decls[ArgIndex] = ParmVarDecl::Create (m_ast_source.m_ast_context, |
| 207 | const_cast<DeclContext*>(m_decl_context), |
| 208 | SourceLocation(), |
Sean Callanan | 279584c | 2011-03-15 00:17:19 +0000 | [diff] [blame] | 209 | SourceLocation(), |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 210 | NULL, |
| 211 | arg_qual_type, |
| 212 | NULL, |
| 213 | SC_Static, |
| 214 | SC_Static, |
| 215 | NULL); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 218 | func_decl->setParams(param_var_decls, NumArgs); |
Sean Callanan | 3c821cc | 2010-06-23 18:58:10 +0000 | [diff] [blame] | 219 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 220 | delete [] param_var_decls; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 223 | m_decls.push_back(func_decl); |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 224 | |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 225 | return func_decl; |
Sean Callanan | 8f0dc34 | 2010-06-22 23:46:24 +0000 | [diff] [blame] | 226 | } |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 227 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 228 | clang::NamedDecl * |
| 229 | NameSearchContext::AddGenericFunDecl() |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 230 | { |
Sean Callanan | ad29309 | 2011-01-18 23:32:05 +0000 | [diff] [blame] | 231 | FunctionProtoType::ExtProtoInfo proto_info; |
| 232 | |
| 233 | proto_info.Variadic = true; |
| 234 | |
Sean Callanan | fb3058e | 2011-05-12 23:54:16 +0000 | [diff] [blame] | 235 | QualType generic_function_type(m_ast_source.m_ast_context.getFunctionType (m_ast_source.m_ast_context.UnknownAnyTy, // result |
| 236 | NULL, // argument types |
| 237 | 0, // number of arguments |
Sean Callanan | ad29309 | 2011-01-18 23:32:05 +0000 | [diff] [blame] | 238 | proto_info)); |
Greg Clayton | 8de27c7 | 2010-10-15 22:48:33 +0000 | [diff] [blame] | 239 | |
Sean Callanan | 0fc7358 | 2010-07-27 00:55:47 +0000 | [diff] [blame] | 240 | return AddFunDecl(generic_function_type.getAsOpaquePtr()); |
| 241 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 242 | |
Greg Clayton | b01000f | 2011-01-17 03:46:26 +0000 | [diff] [blame] | 243 | clang::NamedDecl * |
| 244 | NameSearchContext::AddTypeDecl(void *type) |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 245 | { |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 246 | if (type) |
| 247 | { |
| 248 | QualType qual_type = QualType::getFromOpaquePtr(type); |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 249 | |
Sean Callanan | d5b3c35 | 2011-01-27 04:42:51 +0000 | [diff] [blame] | 250 | if (const TagType *tag_type = dyn_cast<clang::TagType>(qual_type)) |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 251 | { |
| 252 | TagDecl *tag_decl = tag_type->getDecl(); |
| 253 | |
| 254 | m_decls.push_back(tag_decl); |
| 255 | |
| 256 | return tag_decl; |
| 257 | } |
Sean Callanan | d5b3c35 | 2011-01-27 04:42:51 +0000 | [diff] [blame] | 258 | else if (const ObjCObjectType *objc_object_type = dyn_cast<clang::ObjCObjectType>(qual_type)) |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 259 | { |
| 260 | ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface(); |
| 261 | |
| 262 | m_decls.push_back((NamedDecl*)interface_decl); |
| 263 | |
| 264 | return (NamedDecl*)interface_decl; |
| 265 | } |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 266 | } |
Greg Clayton | a1aaaff | 2011-01-23 00:34:52 +0000 | [diff] [blame] | 267 | return NULL; |
Sean Callanan | 93a4b1a | 2010-08-04 01:02:13 +0000 | [diff] [blame] | 268 | } |
Greg Clayton | e6d72ca | 2011-06-25 00:44:06 +0000 | [diff] [blame^] | 269 | |
| 270 | void |
| 271 | NameSearchContext::AddLookupResult (clang::DeclContextLookupConstResult result) |
| 272 | { |
| 273 | for (clang::NamedDecl * const *decl_iterator = result.first; |
| 274 | decl_iterator != result.second; |
| 275 | ++decl_iterator) |
| 276 | m_decls.push_back (*decl_iterator); |
| 277 | } |
| 278 | |
| 279 | void |
| 280 | NameSearchContext::AddNamedDecl (clang::NamedDecl *decl) |
| 281 | { |
| 282 | m_decls.push_back (decl); |
| 283 | } |