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