1 | //===-- ClangASTSource.cpp ------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "ClangASTSource.h" |
10 | |
11 | #include "ClangDeclVendor.h" |
12 | #include "ClangModulesDeclVendor.h" |
13 | |
14 | #include "lldb/Core/Module.h" |
15 | #include "lldb/Core/ModuleList.h" |
16 | #include "lldb/Symbol/CompilerDeclContext.h" |
17 | #include "lldb/Symbol/Function.h" |
18 | #include "lldb/Symbol/SymbolFile.h" |
19 | #include "lldb/Symbol/TaggedASTType.h" |
20 | #include "lldb/Target/Target.h" |
21 | #include "lldb/Utility/LLDBLog.h" |
22 | #include "lldb/Utility/Log.h" |
23 | #include "clang/AST/ASTContext.h" |
24 | #include "clang/Basic/SourceManager.h" |
25 | |
26 | #include "Plugins/ExpressionParser/Clang/ClangUtil.h" |
27 | #include "Plugins/LanguageRuntime/ObjC/ObjCLanguageRuntime.h" |
28 | #include "Plugins/TypeSystem/Clang/TypeSystemClang.h" |
29 | |
30 | #include <memory> |
31 | #include <vector> |
32 | |
33 | using namespace clang; |
34 | using namespace lldb_private; |
35 | |
36 | // Scoped class that will remove an active lexical decl from the set when it |
37 | // goes out of scope. |
38 | namespace { |
39 | class ScopedLexicalDeclEraser { |
40 | public: |
41 | ScopedLexicalDeclEraser(std::set<const clang::Decl *> &decls, |
42 | const clang::Decl *decl) |
43 | : m_active_lexical_decls(decls), m_decl(decl) {} |
44 | |
45 | ~ScopedLexicalDeclEraser() { m_active_lexical_decls.erase(x: m_decl); } |
46 | |
47 | private: |
48 | std::set<const clang::Decl *> &m_active_lexical_decls; |
49 | const clang::Decl *m_decl; |
50 | }; |
51 | } |
52 | |
53 | ClangASTSource::ClangASTSource( |
54 | const lldb::TargetSP &target, |
55 | const std::shared_ptr<ClangASTImporter> &importer) |
56 | : m_lookups_enabled(false), m_target(target), m_ast_context(nullptr), |
57 | m_ast_importer_sp(importer), m_active_lexical_decls(), |
58 | m_active_lookups() { |
59 | assert(m_ast_importer_sp && "No ClangASTImporter passed to ClangASTSource?" ); |
60 | } |
61 | |
62 | void ClangASTSource::InstallASTContext(TypeSystemClang &clang_ast_context) { |
63 | m_ast_context = &clang_ast_context.getASTContext(); |
64 | m_clang_ast_context = &clang_ast_context; |
65 | m_file_manager = &m_ast_context->getSourceManager().getFileManager(); |
66 | m_ast_importer_sp->InstallMapCompleter(dst_ctx: m_ast_context, completer&: *this); |
67 | } |
68 | |
69 | ClangASTSource::~ClangASTSource() { |
70 | m_ast_importer_sp->ForgetDestination(dst_ctx: m_ast_context); |
71 | |
72 | if (!m_target) |
73 | return; |
74 | |
75 | // Unregister the current ASTContext as a source for all scratch |
76 | // ASTContexts in the ClangASTImporter. Without this the scratch AST might |
77 | // query the deleted ASTContext for additional type information. |
78 | // We unregister from *all* scratch ASTContexts in case a type got exported |
79 | // to a scratch AST that isn't the best fitting scratch ASTContext. |
80 | lldb::TypeSystemClangSP scratch_ts_sp = ScratchTypeSystemClang::GetForTarget( |
81 | target&: *m_target, ast_kind: ScratchTypeSystemClang::DefaultAST, create_on_demand: false); |
82 | |
83 | if (!scratch_ts_sp) |
84 | return; |
85 | |
86 | ScratchTypeSystemClang *default_scratch_ast = |
87 | llvm::cast<ScratchTypeSystemClang>(Val: scratch_ts_sp.get()); |
88 | // Unregister from the default scratch AST (and all sub-ASTs). |
89 | default_scratch_ast->ForgetSource(src_ctx: m_ast_context, importer&: *m_ast_importer_sp); |
90 | } |
91 | |
92 | void ClangASTSource::StartTranslationUnit(ASTConsumer *Consumer) { |
93 | if (!m_ast_context) |
94 | return; |
95 | |
96 | m_ast_context->getTranslationUnitDecl()->setHasExternalVisibleStorage(); |
97 | m_ast_context->getTranslationUnitDecl()->setHasExternalLexicalStorage(); |
98 | } |
99 | |
100 | // The core lookup interface. |
101 | bool ClangASTSource::FindExternalVisibleDeclsByName( |
102 | const DeclContext *decl_ctx, DeclarationName clang_decl_name, |
103 | const clang::DeclContext *original_dc) { |
104 | if (!m_ast_context) { |
105 | SetNoExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name); |
106 | return false; |
107 | } |
108 | |
109 | std::string decl_name(clang_decl_name.getAsString()); |
110 | |
111 | switch (clang_decl_name.getNameKind()) { |
112 | // Normal identifiers. |
113 | case DeclarationName::Identifier: { |
114 | clang::IdentifierInfo *identifier_info = |
115 | clang_decl_name.getAsIdentifierInfo(); |
116 | |
117 | if (!identifier_info || identifier_info->getBuiltinID() != 0) { |
118 | SetNoExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name); |
119 | return false; |
120 | } |
121 | } break; |
122 | |
123 | // Operator names. |
124 | case DeclarationName::CXXOperatorName: |
125 | case DeclarationName::CXXLiteralOperatorName: |
126 | break; |
127 | |
128 | // Using directives found in this context. |
129 | // Tell Sema we didn't find any or we'll end up getting asked a *lot*. |
130 | case DeclarationName::CXXUsingDirective: |
131 | SetNoExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name); |
132 | return false; |
133 | |
134 | case DeclarationName::ObjCZeroArgSelector: |
135 | case DeclarationName::ObjCOneArgSelector: |
136 | case DeclarationName::ObjCMultiArgSelector: { |
137 | llvm::SmallVector<NamedDecl *, 1> method_decls; |
138 | |
139 | NameSearchContext method_search_context(*m_clang_ast_context, method_decls, |
140 | clang_decl_name, decl_ctx); |
141 | |
142 | FindObjCMethodDecls(context&: method_search_context); |
143 | |
144 | SetExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name, Decls: method_decls); |
145 | return (method_decls.size() > 0); |
146 | } |
147 | // These aren't possible in the global context. |
148 | case DeclarationName::CXXConstructorName: |
149 | case DeclarationName::CXXDestructorName: |
150 | case DeclarationName::CXXConversionFunctionName: |
151 | case DeclarationName::CXXDeductionGuideName: |
152 | SetNoExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name); |
153 | return false; |
154 | } |
155 | |
156 | if (!GetLookupsEnabled()) { |
157 | // Wait until we see a '$' at the start of a name before we start doing any |
158 | // lookups so we can avoid lookup up all of the builtin types. |
159 | if (!decl_name.empty() && decl_name[0] == '$') { |
160 | SetLookupsEnabled(true); |
161 | } else { |
162 | SetNoExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name); |
163 | return false; |
164 | } |
165 | } |
166 | |
167 | ConstString const_decl_name(decl_name.c_str()); |
168 | |
169 | const char *uniqued_const_decl_name = const_decl_name.GetCString(); |
170 | if (m_active_lookups.find(x: uniqued_const_decl_name) != |
171 | m_active_lookups.end()) { |
172 | // We are currently looking up this name... |
173 | SetNoExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name); |
174 | return false; |
175 | } |
176 | m_active_lookups.insert(x: uniqued_const_decl_name); |
177 | llvm::SmallVector<NamedDecl *, 4> name_decls; |
178 | NameSearchContext name_search_context(*m_clang_ast_context, name_decls, |
179 | clang_decl_name, decl_ctx); |
180 | FindExternalVisibleDecls(context&: name_search_context); |
181 | SetExternalVisibleDeclsForName(DC: decl_ctx, Name: clang_decl_name, Decls: name_decls); |
182 | m_active_lookups.erase(x: uniqued_const_decl_name); |
183 | return (name_decls.size() != 0); |
184 | } |
185 | |
186 | TagDecl *ClangASTSource::FindCompleteType(const TagDecl *decl) { |
187 | Log *log = GetLog(mask: LLDBLog::Expressions); |
188 | |
189 | if (const NamespaceDecl *namespace_context = |
190 | dyn_cast<NamespaceDecl>(decl->getDeclContext())) { |
191 | ClangASTImporter::NamespaceMapSP namespace_map = |
192 | m_ast_importer_sp->GetNamespaceMap(decl: namespace_context); |
193 | |
194 | if (!namespace_map) |
195 | return nullptr; |
196 | |
197 | LLDB_LOGV(log, " CTD Inspecting namespace map{0:x} ({1} entries)" , |
198 | namespace_map.get(), namespace_map->size()); |
199 | |
200 | for (const ClangASTImporter::NamespaceMapItem &item : *namespace_map) { |
201 | LLDB_LOG(log, " CTD Searching namespace {0} in module {1}" , |
202 | item.second.GetName(), item.first->GetFileSpec().GetFilename()); |
203 | |
204 | ConstString name(decl->getName()); |
205 | |
206 | // Create a type matcher using the CompilerDeclContext for the namespace |
207 | // as the context (item.second) and search for the name inside of this |
208 | // context. |
209 | TypeQuery query(item.second, name); |
210 | TypeResults results; |
211 | item.first->FindTypes(query, results); |
212 | |
213 | for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) { |
214 | CompilerType clang_type(type_sp->GetFullCompilerType()); |
215 | |
216 | if (!ClangUtil::IsClangType(clang_type)) |
217 | continue; |
218 | |
219 | const TagType *tag_type = |
220 | ClangUtil::GetQualType(clang_type)->getAs<TagType>(); |
221 | |
222 | if (!tag_type) |
223 | continue; |
224 | |
225 | TagDecl *candidate_tag_decl = |
226 | const_cast<TagDecl *>(tag_type->getDecl()); |
227 | |
228 | if (TypeSystemClang::GetCompleteDecl( |
229 | &candidate_tag_decl->getASTContext(), candidate_tag_decl)) |
230 | return candidate_tag_decl; |
231 | } |
232 | } |
233 | } else { |
234 | const ModuleList &module_list = m_target->GetImages(); |
235 | // Create a type matcher using a CompilerDecl. Each TypeSystem class knows |
236 | // how to fill out a CompilerContext array using a CompilerDecl. |
237 | TypeQuery query(CompilerDecl(m_clang_ast_context, (void *)decl)); |
238 | TypeResults results; |
239 | module_list.FindTypes(search_first: nullptr, query, results); |
240 | for (const lldb::TypeSP &type_sp : results.GetTypeMap().Types()) { |
241 | |
242 | CompilerType clang_type(type_sp->GetFullCompilerType()); |
243 | |
244 | if (!ClangUtil::IsClangType(ct: clang_type)) |
245 | continue; |
246 | |
247 | const TagType *tag_type = |
248 | ClangUtil::GetQualType(ct: clang_type)->getAs<TagType>(); |
249 | |
250 | if (!tag_type) |
251 | continue; |
252 | |
253 | TagDecl *candidate_tag_decl = const_cast<TagDecl *>(tag_type->getDecl()); |
254 | |
255 | if (TypeSystemClang::GetCompleteDecl(&candidate_tag_decl->getASTContext(), |
256 | candidate_tag_decl)) |
257 | return candidate_tag_decl; |
258 | } |
259 | } |
260 | return nullptr; |
261 | } |
262 | |
263 | void ClangASTSource::CompleteType(TagDecl *tag_decl) { |
264 | Log *log = GetLog(mask: LLDBLog::Expressions); |
265 | |
266 | if (log) { |
267 | LLDB_LOG(log, |
268 | " CompleteTagDecl on (ASTContext*){0} Completing " |
269 | "(TagDecl*){1:x} named {2}" , |
270 | m_clang_ast_context->getDisplayName(), tag_decl, |
271 | tag_decl->getName()); |
272 | |
273 | LLDB_LOG(log, " CTD Before:\n{0}" , ClangUtil::DumpDecl(tag_decl)); |
274 | } |
275 | |
276 | auto iter = m_active_lexical_decls.find(tag_decl); |
277 | if (iter != m_active_lexical_decls.end()) |
278 | return; |
279 | m_active_lexical_decls.insert(tag_decl); |
280 | ScopedLexicalDeclEraser eraser(m_active_lexical_decls, tag_decl); |
281 | |
282 | if (!m_ast_importer_sp->CompleteTagDecl(decl: tag_decl)) { |
283 | // We couldn't complete the type. Maybe there's a definition somewhere |
284 | // else that can be completed. |
285 | if (TagDecl *alternate = FindCompleteType(decl: tag_decl)) |
286 | m_ast_importer_sp->CompleteTagDeclWithOrigin(decl: tag_decl, origin: alternate); |
287 | } |
288 | |
289 | LLDB_LOG(log, " [CTD] After:\n{0}" , ClangUtil::DumpDecl(tag_decl)); |
290 | } |
291 | |
292 | void ClangASTSource::CompleteType(clang::ObjCInterfaceDecl *interface_decl) { |
293 | Log *log = GetLog(mask: LLDBLog::Expressions); |
294 | |
295 | LLDB_LOG(log, |
296 | " [CompleteObjCInterfaceDecl] on (ASTContext*){0:x} '{1}' " |
297 | "Completing an ObjCInterfaceDecl named {2}" , |
298 | m_ast_context, m_clang_ast_context->getDisplayName(), |
299 | interface_decl->getName()); |
300 | LLDB_LOG(log, " [COID] Before:\n{0}" , |
301 | ClangUtil::DumpDecl(interface_decl)); |
302 | |
303 | ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl); |
304 | |
305 | if (original.Valid()) { |
306 | if (ObjCInterfaceDecl *original_iface_decl = |
307 | dyn_cast<ObjCInterfaceDecl>(Val: original.decl)) { |
308 | ObjCInterfaceDecl *complete_iface_decl = |
309 | GetCompleteObjCInterface(interface_decl: original_iface_decl); |
310 | |
311 | if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) { |
312 | m_ast_importer_sp->SetDeclOrigin(interface_decl, complete_iface_decl); |
313 | } |
314 | } |
315 | } |
316 | |
317 | m_ast_importer_sp->CompleteObjCInterfaceDecl(interface_decl); |
318 | |
319 | if (interface_decl->getSuperClass() && |
320 | interface_decl->getSuperClass() != interface_decl) |
321 | CompleteType(interface_decl: interface_decl->getSuperClass()); |
322 | |
323 | LLDB_LOG(log, " [COID] After:" ); |
324 | LLDB_LOG(log, " [COID] {0}" , ClangUtil::DumpDecl(interface_decl)); |
325 | } |
326 | |
327 | clang::ObjCInterfaceDecl *ClangASTSource::GetCompleteObjCInterface( |
328 | const clang::ObjCInterfaceDecl *interface_decl) { |
329 | lldb::ProcessSP process(m_target->GetProcessSP()); |
330 | |
331 | if (!process) |
332 | return nullptr; |
333 | |
334 | ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(process&: *process)); |
335 | |
336 | if (!language_runtime) |
337 | return nullptr; |
338 | |
339 | ConstString class_name(interface_decl->getNameAsString().c_str()); |
340 | |
341 | lldb::TypeSP complete_type_sp( |
342 | language_runtime->LookupInCompleteClassCache(name&: class_name)); |
343 | |
344 | if (!complete_type_sp) |
345 | return nullptr; |
346 | |
347 | TypeFromUser complete_type = |
348 | TypeFromUser(complete_type_sp->GetFullCompilerType()); |
349 | lldb::opaque_compiler_type_t complete_opaque_type = |
350 | complete_type.GetOpaqueQualType(); |
351 | |
352 | if (!complete_opaque_type) |
353 | return nullptr; |
354 | |
355 | const clang::Type *complete_clang_type = |
356 | QualType::getFromOpaquePtr(Ptr: complete_opaque_type).getTypePtr(); |
357 | const ObjCInterfaceType *complete_interface_type = |
358 | dyn_cast<ObjCInterfaceType>(Val: complete_clang_type); |
359 | |
360 | if (!complete_interface_type) |
361 | return nullptr; |
362 | |
363 | ObjCInterfaceDecl *complete_iface_decl(complete_interface_type->getDecl()); |
364 | |
365 | return complete_iface_decl; |
366 | } |
367 | |
368 | void ClangASTSource::FindExternalLexicalDecls( |
369 | const DeclContext *decl_context, |
370 | llvm::function_ref<bool(Decl::Kind)> predicate, |
371 | llvm::SmallVectorImpl<Decl *> &decls) { |
372 | |
373 | Log *log = GetLog(mask: LLDBLog::Expressions); |
374 | |
375 | const Decl *context_decl = dyn_cast<Decl>(Val: decl_context); |
376 | |
377 | if (!context_decl) |
378 | return; |
379 | |
380 | auto iter = m_active_lexical_decls.find(x: context_decl); |
381 | if (iter != m_active_lexical_decls.end()) |
382 | return; |
383 | m_active_lexical_decls.insert(x: context_decl); |
384 | ScopedLexicalDeclEraser eraser(m_active_lexical_decls, context_decl); |
385 | |
386 | if (log) { |
387 | if (const NamedDecl *context_named_decl = dyn_cast<NamedDecl>(Val: context_decl)) |
388 | LLDB_LOG(log, |
389 | "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in " |
390 | "'{2}' ({3}Decl*){4}" , |
391 | m_ast_context, m_clang_ast_context->getDisplayName(), |
392 | context_named_decl->getNameAsString().c_str(), |
393 | context_decl->getDeclKindName(), |
394 | static_cast<const void *>(context_decl)); |
395 | else if (context_decl) |
396 | LLDB_LOG(log, |
397 | "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in " |
398 | "({2}Decl*){3}" , |
399 | m_ast_context, m_clang_ast_context->getDisplayName(), |
400 | context_decl->getDeclKindName(), |
401 | static_cast<const void *>(context_decl)); |
402 | else |
403 | LLDB_LOG(log, |
404 | "FindExternalLexicalDecls on (ASTContext*){0:x} '{1}' in a " |
405 | "NULL context" , |
406 | m_ast_context, m_clang_ast_context->getDisplayName()); |
407 | } |
408 | |
409 | ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(decl: context_decl); |
410 | |
411 | if (!original.Valid()) |
412 | return; |
413 | |
414 | LLDB_LOG(log, " FELD Original decl (ASTContext*){0:x} (Decl*){1:x}:\n{2}" , |
415 | static_cast<void *>(original.ctx), |
416 | static_cast<void *>(original.decl), |
417 | ClangUtil::DumpDecl(original.decl)); |
418 | |
419 | if (ObjCInterfaceDecl *original_iface_decl = |
420 | dyn_cast<ObjCInterfaceDecl>(Val: original.decl)) { |
421 | ObjCInterfaceDecl *complete_iface_decl = |
422 | GetCompleteObjCInterface(interface_decl: original_iface_decl); |
423 | |
424 | if (complete_iface_decl && (complete_iface_decl != original_iface_decl)) { |
425 | original.decl = complete_iface_decl; |
426 | original.ctx = &complete_iface_decl->getASTContext(); |
427 | |
428 | m_ast_importer_sp->SetDeclOrigin(context_decl, complete_iface_decl); |
429 | } |
430 | } |
431 | |
432 | if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(Val: original.decl)) { |
433 | ExternalASTSource *external_source = original.ctx->getExternalSource(); |
434 | |
435 | if (external_source) |
436 | external_source->CompleteType(Tag: original_tag_decl); |
437 | } |
438 | |
439 | const DeclContext *original_decl_context = |
440 | dyn_cast<DeclContext>(Val: original.decl); |
441 | |
442 | if (!original_decl_context) |
443 | return; |
444 | |
445 | // Indicates whether we skipped any Decls of the original DeclContext. |
446 | bool SkippedDecls = false; |
447 | for (Decl *decl : original_decl_context->decls()) { |
448 | // The predicate function returns true if the passed declaration kind is |
449 | // the one we are looking for. |
450 | // See clang::ExternalASTSource::FindExternalLexicalDecls() |
451 | if (predicate(decl->getKind())) { |
452 | if (log) { |
453 | std::string ast_dump = ClangUtil::DumpDecl(d: decl); |
454 | if (const NamedDecl *context_named_decl = |
455 | dyn_cast<NamedDecl>(Val: context_decl)) |
456 | LLDB_LOG(log, " FELD Adding [to {0}Decl {1}] lexical {2}Decl {3}" , |
457 | context_named_decl->getDeclKindName(), |
458 | context_named_decl->getName(), decl->getDeclKindName(), |
459 | ast_dump); |
460 | else |
461 | LLDB_LOG(log, " FELD Adding lexical {0}Decl {1}" , |
462 | decl->getDeclKindName(), ast_dump); |
463 | } |
464 | |
465 | Decl *copied_decl = CopyDecl(src_decl: decl); |
466 | |
467 | if (!copied_decl) |
468 | continue; |
469 | |
470 | // FIXME: We should add the copied decl to the 'decls' list. This would |
471 | // add the copied Decl into the DeclContext and make sure that we |
472 | // correctly propagate that we added some Decls back to Clang. |
473 | // By leaving 'decls' empty we incorrectly return false from |
474 | // DeclContext::LoadLexicalDeclsFromExternalStorage which might cause |
475 | // lookup issues later on. |
476 | // We can't just add them for now as the ASTImporter already added the |
477 | // decl into the DeclContext and this would add it twice. |
478 | |
479 | if (FieldDecl *copied_field = dyn_cast<FieldDecl>(Val: copied_decl)) { |
480 | QualType copied_field_type = copied_field->getType(); |
481 | |
482 | m_ast_importer_sp->RequireCompleteType(type: copied_field_type); |
483 | } |
484 | } else { |
485 | SkippedDecls = true; |
486 | } |
487 | } |
488 | |
489 | // CopyDecl may build a lookup table which may set up ExternalLexicalStorage |
490 | // to false. However, since we skipped some of the external Decls we must |
491 | // set it back! |
492 | if (SkippedDecls) { |
493 | decl_context->setHasExternalLexicalStorage(true); |
494 | // This sets HasLazyExternalLexicalLookups to true. By setting this bit we |
495 | // ensure that the lookup table is rebuilt, which means the external source |
496 | // is consulted again when a clang::DeclContext::lookup is called. |
497 | const_cast<DeclContext *>(decl_context)->setMustBuildLookupTable(); |
498 | } |
499 | } |
500 | |
501 | void ClangASTSource::FindExternalVisibleDecls(NameSearchContext &context) { |
502 | assert(m_ast_context); |
503 | |
504 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
505 | |
506 | Log *log = GetLog(mask: LLDBLog::Expressions); |
507 | |
508 | if (log) { |
509 | if (!context.m_decl_context) |
510 | LLDB_LOG(log, |
511 | "ClangASTSource::FindExternalVisibleDecls on " |
512 | "(ASTContext*){0:x} '{1}' for '{2}' in a NULL DeclContext" , |
513 | m_ast_context, m_clang_ast_context->getDisplayName(), name); |
514 | else if (const NamedDecl *context_named_decl = |
515 | dyn_cast<NamedDecl>(Val: context.m_decl_context)) |
516 | LLDB_LOG(log, |
517 | "ClangASTSource::FindExternalVisibleDecls on " |
518 | "(ASTContext*){0:x} '{1}' for '{2}' in '{3}'" , |
519 | m_ast_context, m_clang_ast_context->getDisplayName(), name, |
520 | context_named_decl->getName()); |
521 | else |
522 | LLDB_LOG(log, |
523 | "ClangASTSource::FindExternalVisibleDecls on " |
524 | "(ASTContext*){0:x} '{1}' for '{2}' in a '{3}'" , |
525 | m_ast_context, m_clang_ast_context->getDisplayName(), name, |
526 | context.m_decl_context->getDeclKindName()); |
527 | } |
528 | |
529 | if (isa<NamespaceDecl>(Val: context.m_decl_context)) { |
530 | LookupInNamespace(context); |
531 | } else if (isa<ObjCInterfaceDecl>(Val: context.m_decl_context)) { |
532 | FindObjCPropertyAndIvarDecls(context); |
533 | } else if (!isa<TranslationUnitDecl>(Val: context.m_decl_context)) { |
534 | // we shouldn't be getting FindExternalVisibleDecls calls for these |
535 | return; |
536 | } else { |
537 | CompilerDeclContext namespace_decl; |
538 | |
539 | LLDB_LOG(log, " CAS::FEVD Searching the root namespace" ); |
540 | |
541 | FindExternalVisibleDecls(context, module: lldb::ModuleSP(), namespace_decl); |
542 | } |
543 | |
544 | if (!context.m_namespace_map->empty()) { |
545 | if (log && log->GetVerbose()) |
546 | LLDB_LOG(log, " CAS::FEVD Registering namespace map {0:x} ({1} entries)" , |
547 | context.m_namespace_map.get(), context.m_namespace_map->size()); |
548 | |
549 | NamespaceDecl *clang_namespace_decl = |
550 | AddNamespace(context, namespace_decls&: context.m_namespace_map); |
551 | |
552 | if (clang_namespace_decl) |
553 | clang_namespace_decl->setHasExternalVisibleStorage(); |
554 | } |
555 | } |
556 | |
557 | clang::Sema *ClangASTSource::getSema() { |
558 | return m_clang_ast_context->getSema(); |
559 | } |
560 | |
561 | bool ClangASTSource::IgnoreName(const ConstString name, |
562 | bool ignore_all_dollar_names) { |
563 | static const ConstString id_name("id" ); |
564 | static const ConstString Class_name("Class" ); |
565 | |
566 | if (m_ast_context->getLangOpts().ObjC) |
567 | if (name == id_name || name == Class_name) |
568 | return true; |
569 | |
570 | StringRef name_string_ref = name.GetStringRef(); |
571 | |
572 | // The ClangASTSource is not responsible for finding $-names. |
573 | return name_string_ref.empty() || |
574 | (ignore_all_dollar_names && name_string_ref.starts_with(Prefix: "$" )) || |
575 | name_string_ref.starts_with(Prefix: "_$" ); |
576 | } |
577 | |
578 | void ClangASTSource::FindExternalVisibleDecls( |
579 | NameSearchContext &context, lldb::ModuleSP module_sp, |
580 | CompilerDeclContext &namespace_decl) { |
581 | assert(m_ast_context); |
582 | |
583 | Log *log = GetLog(mask: LLDBLog::Expressions); |
584 | |
585 | SymbolContextList sc_list; |
586 | |
587 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
588 | if (IgnoreName(name, ignore_all_dollar_names: true)) |
589 | return; |
590 | |
591 | if (!m_target) |
592 | return; |
593 | |
594 | FillNamespaceMap(context, module_sp, namespace_decl); |
595 | |
596 | if (context.m_found_type) |
597 | return; |
598 | |
599 | lldb::TypeSP type_sp; |
600 | TypeResults results; |
601 | if (module_sp && namespace_decl) { |
602 | // Match the name in the specified decl context. |
603 | TypeQuery query(namespace_decl, name, TypeQueryOptions::e_find_one); |
604 | module_sp->FindTypes(query, results); |
605 | type_sp = results.GetFirstType(); |
606 | } else { |
607 | // Match the exact name of the type at the root level. |
608 | TypeQuery query(name.GetStringRef(), TypeQueryOptions::e_exact_match | |
609 | TypeQueryOptions::e_find_one); |
610 | m_target->GetImages().FindTypes(search_first: nullptr, query, results); |
611 | type_sp = results.GetFirstType(); |
612 | } |
613 | |
614 | if (type_sp) { |
615 | if (log) { |
616 | const char *name_string = type_sp->GetName().GetCString(); |
617 | |
618 | LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\": {1}" , name, |
619 | (name_string ? name_string : "<anonymous>" )); |
620 | } |
621 | |
622 | CompilerType full_type = type_sp->GetFullCompilerType(); |
623 | |
624 | CompilerType copied_clang_type(GuardedCopyType(src_type: full_type)); |
625 | |
626 | if (!copied_clang_type) { |
627 | LLDB_LOG(log, " CAS::FEVD - Couldn't export a type" ); |
628 | } else { |
629 | |
630 | context.AddTypeDecl(compiler_type: copied_clang_type); |
631 | |
632 | context.m_found_type = true; |
633 | } |
634 | } |
635 | |
636 | if (!context.m_found_type) { |
637 | // Try the modules next. |
638 | FindDeclInModules(context, name); |
639 | } |
640 | |
641 | if (!context.m_found_type && m_ast_context->getLangOpts().ObjC) { |
642 | FindDeclInObjCRuntime(context, name); |
643 | } |
644 | } |
645 | |
646 | void ClangASTSource::FillNamespaceMap( |
647 | NameSearchContext &context, lldb::ModuleSP module_sp, |
648 | const CompilerDeclContext &namespace_decl) { |
649 | const ConstString name(context.m_decl_name.getAsString().c_str()); |
650 | if (IgnoreName(name, ignore_all_dollar_names: true)) |
651 | return; |
652 | |
653 | Log *log = GetLog(mask: LLDBLog::Expressions); |
654 | |
655 | if (module_sp && namespace_decl) { |
656 | CompilerDeclContext found_namespace_decl; |
657 | |
658 | if (SymbolFile *symbol_file = module_sp->GetSymbolFile()) { |
659 | found_namespace_decl = symbol_file->FindNamespace(name, parent_decl_ctx: namespace_decl); |
660 | |
661 | if (found_namespace_decl) { |
662 | context.m_namespace_map->push_back( |
663 | x: std::pair<lldb::ModuleSP, CompilerDeclContext>( |
664 | module_sp, found_namespace_decl)); |
665 | |
666 | LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}" , name, |
667 | module_sp->GetFileSpec().GetFilename()); |
668 | } |
669 | } |
670 | return; |
671 | } |
672 | |
673 | for (lldb::ModuleSP image : m_target->GetImages().Modules()) { |
674 | if (!image) |
675 | continue; |
676 | |
677 | CompilerDeclContext found_namespace_decl; |
678 | |
679 | SymbolFile *symbol_file = image->GetSymbolFile(); |
680 | |
681 | if (!symbol_file) |
682 | continue; |
683 | |
684 | // If namespace_decl is not valid, 'FindNamespace' would look for |
685 | // any namespace called 'name' (ignoring parent contexts) and return |
686 | // the first one it finds. Thus if we're doing a qualified lookup only |
687 | // consider root namespaces. E.g., in an expression ::A::B::Foo, the |
688 | // lookup of ::A will result in a qualified lookup. Note, namespace |
689 | // disambiguation for function calls are handled separately in |
690 | // SearchFunctionsInSymbolContexts. |
691 | const bool find_root_namespaces = |
692 | context.m_decl_context && |
693 | context.m_decl_context->shouldUseQualifiedLookup(); |
694 | found_namespace_decl = symbol_file->FindNamespace( |
695 | name, parent_decl_ctx: namespace_decl, /* only root namespaces */ only_root_namespaces: find_root_namespaces); |
696 | |
697 | if (found_namespace_decl) { |
698 | context.m_namespace_map->push_back( |
699 | x: std::pair<lldb::ModuleSP, CompilerDeclContext>(image, |
700 | found_namespace_decl)); |
701 | |
702 | LLDB_LOG(log, " CAS::FEVD Found namespace {0} in module {1}" , name, |
703 | image->GetFileSpec().GetFilename()); |
704 | } |
705 | } |
706 | } |
707 | |
708 | bool ClangASTSource::FindObjCMethodDeclsWithOrigin( |
709 | NameSearchContext &context, ObjCInterfaceDecl *original_interface_decl, |
710 | const char *log_info) { |
711 | const DeclarationName &decl_name(context.m_decl_name); |
712 | clang::ASTContext *original_ctx = &original_interface_decl->getASTContext(); |
713 | |
714 | Selector original_selector; |
715 | |
716 | if (decl_name.isObjCZeroArgSelector()) { |
717 | const IdentifierInfo *ident = |
718 | &original_ctx->Idents.get(Name: decl_name.getAsString()); |
719 | original_selector = original_ctx->Selectors.getSelector(NumArgs: 0, IIV: &ident); |
720 | } else if (decl_name.isObjCOneArgSelector()) { |
721 | const std::string &decl_name_string = decl_name.getAsString(); |
722 | std::string decl_name_string_without_colon(decl_name_string.c_str(), |
723 | decl_name_string.length() - 1); |
724 | const IdentifierInfo *ident = |
725 | &original_ctx->Idents.get(Name: decl_name_string_without_colon); |
726 | original_selector = original_ctx->Selectors.getSelector(NumArgs: 1, IIV: &ident); |
727 | } else { |
728 | SmallVector<const IdentifierInfo *, 4> idents; |
729 | |
730 | clang::Selector sel = decl_name.getObjCSelector(); |
731 | |
732 | unsigned num_args = sel.getNumArgs(); |
733 | |
734 | for (unsigned i = 0; i != num_args; ++i) { |
735 | idents.push_back(Elt: &original_ctx->Idents.get(Name: sel.getNameForSlot(argIndex: i))); |
736 | } |
737 | |
738 | original_selector = |
739 | original_ctx->Selectors.getSelector(NumArgs: num_args, IIV: idents.data()); |
740 | } |
741 | |
742 | DeclarationName original_decl_name(original_selector); |
743 | |
744 | llvm::SmallVector<NamedDecl *, 1> methods; |
745 | |
746 | TypeSystemClang::GetCompleteDecl(original_ctx, original_interface_decl); |
747 | |
748 | if (ObjCMethodDecl *instance_method_decl = |
749 | original_interface_decl->lookupInstanceMethod(Sel: original_selector)) { |
750 | methods.push_back(instance_method_decl); |
751 | } else if (ObjCMethodDecl *class_method_decl = |
752 | original_interface_decl->lookupClassMethod( |
753 | Sel: original_selector)) { |
754 | methods.push_back(class_method_decl); |
755 | } |
756 | |
757 | if (methods.empty()) { |
758 | return false; |
759 | } |
760 | |
761 | for (NamedDecl *named_decl : methods) { |
762 | if (!named_decl) |
763 | continue; |
764 | |
765 | ObjCMethodDecl *result_method = dyn_cast<ObjCMethodDecl>(Val: named_decl); |
766 | |
767 | if (!result_method) |
768 | continue; |
769 | |
770 | Decl *copied_decl = CopyDecl(result_method); |
771 | |
772 | if (!copied_decl) |
773 | continue; |
774 | |
775 | ObjCMethodDecl *copied_method_decl = dyn_cast<ObjCMethodDecl>(Val: copied_decl); |
776 | |
777 | if (!copied_method_decl) |
778 | continue; |
779 | |
780 | Log *log = GetLog(mask: LLDBLog::Expressions); |
781 | |
782 | LLDB_LOG(log, " CAS::FOMD found ({0}) {1}" , log_info, |
783 | ClangUtil::DumpDecl(copied_method_decl)); |
784 | |
785 | context.AddNamedDecl(copied_method_decl); |
786 | } |
787 | |
788 | return true; |
789 | } |
790 | |
791 | void ClangASTSource::FindDeclInModules(NameSearchContext &context, |
792 | ConstString name) { |
793 | Log *log = GetLog(mask: LLDBLog::Expressions); |
794 | |
795 | std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor = |
796 | GetClangModulesDeclVendor(); |
797 | if (!modules_decl_vendor) |
798 | return; |
799 | |
800 | bool append = false; |
801 | uint32_t max_matches = 1; |
802 | std::vector<clang::NamedDecl *> decls; |
803 | |
804 | if (!modules_decl_vendor->FindDecls(name, append, max_matches, decls)) |
805 | return; |
806 | |
807 | LLDB_LOG(log, " CAS::FEVD Matching entity found for \"{0}\" in the modules" , |
808 | name); |
809 | |
810 | clang::NamedDecl *const decl_from_modules = decls[0]; |
811 | |
812 | if (llvm::isa<clang::TypeDecl>(Val: decl_from_modules) || |
813 | llvm::isa<clang::ObjCContainerDecl>(Val: decl_from_modules) || |
814 | llvm::isa<clang::EnumConstantDecl>(Val: decl_from_modules)) { |
815 | clang::Decl *copied_decl = CopyDecl(decl_from_modules); |
816 | clang::NamedDecl *copied_named_decl = |
817 | copied_decl ? dyn_cast<clang::NamedDecl>(Val: copied_decl) : nullptr; |
818 | |
819 | if (!copied_named_decl) { |
820 | LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the modules" ); |
821 | |
822 | return; |
823 | } |
824 | |
825 | context.AddNamedDecl(decl: copied_named_decl); |
826 | |
827 | context.m_found_type = true; |
828 | } |
829 | } |
830 | |
831 | void ClangASTSource::FindDeclInObjCRuntime(NameSearchContext &context, |
832 | ConstString name) { |
833 | Log *log = GetLog(mask: LLDBLog::Expressions); |
834 | |
835 | lldb::ProcessSP process(m_target->GetProcessSP()); |
836 | |
837 | if (!process) |
838 | return; |
839 | |
840 | ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(process&: *process)); |
841 | |
842 | if (!language_runtime) |
843 | return; |
844 | |
845 | DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); |
846 | |
847 | if (!decl_vendor) |
848 | return; |
849 | |
850 | bool append = false; |
851 | uint32_t max_matches = 1; |
852 | std::vector<clang::NamedDecl *> decls; |
853 | |
854 | auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(Val: decl_vendor); |
855 | if (!clang_decl_vendor->FindDecls(name, append, max_matches, decls)) |
856 | return; |
857 | |
858 | LLDB_LOG(log, " CAS::FEVD Matching type found for \"{0}\" in the runtime" , |
859 | name); |
860 | |
861 | clang::Decl *copied_decl = CopyDecl(decls[0]); |
862 | clang::NamedDecl *copied_named_decl = |
863 | copied_decl ? dyn_cast<clang::NamedDecl>(Val: copied_decl) : nullptr; |
864 | |
865 | if (!copied_named_decl) { |
866 | LLDB_LOG(log, " CAS::FEVD - Couldn't export a type from the runtime" ); |
867 | |
868 | return; |
869 | } |
870 | |
871 | context.AddNamedDecl(decl: copied_named_decl); |
872 | } |
873 | |
874 | void ClangASTSource::FindObjCMethodDecls(NameSearchContext &context) { |
875 | Log *log = GetLog(mask: LLDBLog::Expressions); |
876 | |
877 | const DeclarationName &decl_name(context.m_decl_name); |
878 | const DeclContext *decl_ctx(context.m_decl_context); |
879 | |
880 | const ObjCInterfaceDecl *interface_decl = |
881 | dyn_cast<ObjCInterfaceDecl>(Val: decl_ctx); |
882 | |
883 | if (!interface_decl) |
884 | return; |
885 | |
886 | do { |
887 | ClangASTImporter::DeclOrigin original = m_ast_importer_sp->GetDeclOrigin(interface_decl); |
888 | |
889 | if (!original.Valid()) |
890 | break; |
891 | |
892 | ObjCInterfaceDecl *original_interface_decl = |
893 | dyn_cast<ObjCInterfaceDecl>(Val: original.decl); |
894 | |
895 | if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl, |
896 | log_info: "at origin" )) |
897 | return; // found it, no need to look any further |
898 | } while (false); |
899 | |
900 | StreamString ss; |
901 | |
902 | if (decl_name.isObjCZeroArgSelector()) { |
903 | ss.Printf(format: "%s" , decl_name.getAsString().c_str()); |
904 | } else if (decl_name.isObjCOneArgSelector()) { |
905 | ss.Printf(format: "%s" , decl_name.getAsString().c_str()); |
906 | } else { |
907 | clang::Selector sel = decl_name.getObjCSelector(); |
908 | |
909 | for (unsigned i = 0, e = sel.getNumArgs(); i != e; ++i) { |
910 | llvm::StringRef r = sel.getNameForSlot(argIndex: i); |
911 | ss.Printf(format: "%s:" , r.str().c_str()); |
912 | } |
913 | } |
914 | ss.Flush(); |
915 | |
916 | if (ss.GetString().contains(Other: "$__lldb" )) |
917 | return; // we don't need any results |
918 | |
919 | ConstString selector_name(ss.GetString()); |
920 | |
921 | LLDB_LOG(log, |
922 | "ClangASTSource::FindObjCMethodDecls on (ASTContext*){0:x} '{1}' " |
923 | "for selector [{2} {3}]" , |
924 | m_ast_context, m_clang_ast_context->getDisplayName(), |
925 | interface_decl->getName(), selector_name); |
926 | SymbolContextList sc_list; |
927 | |
928 | ModuleFunctionSearchOptions function_options; |
929 | function_options.include_symbols = false; |
930 | function_options.include_inlines = false; |
931 | |
932 | std::string interface_name = interface_decl->getNameAsString(); |
933 | |
934 | do { |
935 | StreamString ms; |
936 | ms.Printf(format: "-[%s %s]" , interface_name.c_str(), selector_name.AsCString()); |
937 | ms.Flush(); |
938 | ConstString instance_method_name(ms.GetString()); |
939 | |
940 | sc_list.Clear(); |
941 | m_target->GetImages().FindFunctions(name: instance_method_name, |
942 | name_type_mask: lldb::eFunctionNameTypeFull, |
943 | options: function_options, sc_list); |
944 | |
945 | if (sc_list.GetSize()) |
946 | break; |
947 | |
948 | ms.Clear(); |
949 | ms.Printf(format: "+[%s %s]" , interface_name.c_str(), selector_name.AsCString()); |
950 | ms.Flush(); |
951 | ConstString class_method_name(ms.GetString()); |
952 | |
953 | sc_list.Clear(); |
954 | m_target->GetImages().FindFunctions(name: class_method_name, |
955 | name_type_mask: lldb::eFunctionNameTypeFull, |
956 | options: function_options, sc_list); |
957 | |
958 | if (sc_list.GetSize()) |
959 | break; |
960 | |
961 | // Fall back and check for methods in categories. If we find methods this |
962 | // way, we need to check that they're actually in categories on the desired |
963 | // class. |
964 | |
965 | SymbolContextList candidate_sc_list; |
966 | |
967 | m_target->GetImages().FindFunctions(name: selector_name, |
968 | name_type_mask: lldb::eFunctionNameTypeSelector, |
969 | options: function_options, sc_list&: candidate_sc_list); |
970 | |
971 | for (const SymbolContext &candidate_sc : candidate_sc_list) { |
972 | if (!candidate_sc.function) |
973 | continue; |
974 | |
975 | const char *candidate_name = candidate_sc.function->GetName().AsCString(); |
976 | |
977 | const char *cursor = candidate_name; |
978 | |
979 | if (*cursor != '+' && *cursor != '-') |
980 | continue; |
981 | |
982 | ++cursor; |
983 | |
984 | if (*cursor != '[') |
985 | continue; |
986 | |
987 | ++cursor; |
988 | |
989 | size_t interface_len = interface_name.length(); |
990 | |
991 | if (strncmp(s1: cursor, s2: interface_name.c_str(), n: interface_len)) |
992 | continue; |
993 | |
994 | cursor += interface_len; |
995 | |
996 | if (*cursor == ' ' || *cursor == '(') |
997 | sc_list.Append(sc: candidate_sc); |
998 | } |
999 | } while (false); |
1000 | |
1001 | if (sc_list.GetSize()) { |
1002 | // We found a good function symbol. Use that. |
1003 | |
1004 | for (const SymbolContext &sc : sc_list) { |
1005 | if (!sc.function) |
1006 | continue; |
1007 | |
1008 | CompilerDeclContext function_decl_ctx = sc.function->GetDeclContext(); |
1009 | if (!function_decl_ctx) |
1010 | continue; |
1011 | |
1012 | ObjCMethodDecl *method_decl = |
1013 | TypeSystemClang::DeclContextGetAsObjCMethodDecl(dc: function_decl_ctx); |
1014 | |
1015 | if (!method_decl) |
1016 | continue; |
1017 | |
1018 | ObjCInterfaceDecl *found_interface_decl = |
1019 | method_decl->getClassInterface(); |
1020 | |
1021 | if (!found_interface_decl) |
1022 | continue; |
1023 | |
1024 | if (found_interface_decl->getName() == interface_decl->getName()) { |
1025 | Decl *copied_decl = CopyDecl(method_decl); |
1026 | |
1027 | if (!copied_decl) |
1028 | continue; |
1029 | |
1030 | ObjCMethodDecl *copied_method_decl = |
1031 | dyn_cast<ObjCMethodDecl>(Val: copied_decl); |
1032 | |
1033 | if (!copied_method_decl) |
1034 | continue; |
1035 | |
1036 | LLDB_LOG(log, " CAS::FOMD found (in symbols)\n{0}" , |
1037 | ClangUtil::DumpDecl(copied_method_decl)); |
1038 | |
1039 | context.AddNamedDecl(copied_method_decl); |
1040 | } |
1041 | } |
1042 | |
1043 | return; |
1044 | } |
1045 | |
1046 | // Try the debug information. |
1047 | |
1048 | do { |
1049 | ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( |
1050 | interface_decl: const_cast<ObjCInterfaceDecl *>(interface_decl)); |
1051 | |
1052 | if (!complete_interface_decl) |
1053 | break; |
1054 | |
1055 | // We found the complete interface. The runtime never needs to be queried |
1056 | // in this scenario. |
1057 | |
1058 | DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( |
1059 | complete_interface_decl); |
1060 | |
1061 | if (complete_interface_decl == interface_decl) |
1062 | break; // already checked this one |
1063 | |
1064 | LLDB_LOG(log, |
1065 | "CAS::FOPD trying origin " |
1066 | "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}..." , |
1067 | complete_interface_decl, &complete_iface_decl->getASTContext()); |
1068 | |
1069 | FindObjCMethodDeclsWithOrigin(context, original_interface_decl: complete_interface_decl, |
1070 | log_info: "in debug info" ); |
1071 | |
1072 | return; |
1073 | } while (false); |
1074 | |
1075 | do { |
1076 | // Check the modules only if the debug information didn't have a complete |
1077 | // interface. |
1078 | |
1079 | if (std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor = |
1080 | GetClangModulesDeclVendor()) { |
1081 | ConstString interface_name(interface_decl->getNameAsString().c_str()); |
1082 | bool append = false; |
1083 | uint32_t max_matches = 1; |
1084 | std::vector<clang::NamedDecl *> decls; |
1085 | |
1086 | if (!modules_decl_vendor->FindDecls(name: interface_name, append, max_matches, |
1087 | decls)) |
1088 | break; |
1089 | |
1090 | ObjCInterfaceDecl *interface_decl_from_modules = |
1091 | dyn_cast<ObjCInterfaceDecl>(Val: decls[0]); |
1092 | |
1093 | if (!interface_decl_from_modules) |
1094 | break; |
1095 | |
1096 | if (FindObjCMethodDeclsWithOrigin(context, original_interface_decl: interface_decl_from_modules, |
1097 | log_info: "in modules" )) |
1098 | return; |
1099 | } |
1100 | } while (false); |
1101 | |
1102 | do { |
1103 | // Check the runtime only if the debug information didn't have a complete |
1104 | // interface and the modules don't get us anywhere. |
1105 | |
1106 | lldb::ProcessSP process(m_target->GetProcessSP()); |
1107 | |
1108 | if (!process) |
1109 | break; |
1110 | |
1111 | ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(process&: *process)); |
1112 | |
1113 | if (!language_runtime) |
1114 | break; |
1115 | |
1116 | DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); |
1117 | |
1118 | if (!decl_vendor) |
1119 | break; |
1120 | |
1121 | ConstString interface_name(interface_decl->getNameAsString().c_str()); |
1122 | bool append = false; |
1123 | uint32_t max_matches = 1; |
1124 | std::vector<clang::NamedDecl *> decls; |
1125 | |
1126 | auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(Val: decl_vendor); |
1127 | if (!clang_decl_vendor->FindDecls(name: interface_name, append, max_matches, |
1128 | decls)) |
1129 | break; |
1130 | |
1131 | ObjCInterfaceDecl *runtime_interface_decl = |
1132 | dyn_cast<ObjCInterfaceDecl>(Val: decls[0]); |
1133 | |
1134 | if (!runtime_interface_decl) |
1135 | break; |
1136 | |
1137 | FindObjCMethodDeclsWithOrigin(context, original_interface_decl: runtime_interface_decl, |
1138 | log_info: "in runtime" ); |
1139 | } while (false); |
1140 | } |
1141 | |
1142 | bool ClangASTSource::FindObjCPropertyAndIvarDeclsWithOrigin( |
1143 | NameSearchContext &context, |
1144 | DeclFromUser<const ObjCInterfaceDecl> &origin_iface_decl) { |
1145 | Log *log = GetLog(mask: LLDBLog::Expressions); |
1146 | |
1147 | if (origin_iface_decl.IsInvalid()) |
1148 | return false; |
1149 | |
1150 | std::string name_str = context.m_decl_name.getAsString(); |
1151 | StringRef name(name_str); |
1152 | IdentifierInfo &name_identifier( |
1153 | origin_iface_decl->getASTContext().Idents.get(name)); |
1154 | |
1155 | DeclFromUser<ObjCPropertyDecl> origin_property_decl( |
1156 | origin_iface_decl->FindPropertyDeclaration( |
1157 | &name_identifier, ObjCPropertyQueryKind::OBJC_PR_query_instance)); |
1158 | |
1159 | bool found = false; |
1160 | |
1161 | if (origin_property_decl.IsValid()) { |
1162 | DeclFromParser<ObjCPropertyDecl> parser_property_decl( |
1163 | origin_property_decl.Import(dest_ctx: m_ast_context, importer&: *m_ast_importer_sp)); |
1164 | if (parser_property_decl.IsValid()) { |
1165 | LLDB_LOG(log, " CAS::FOPD found\n{0}" , |
1166 | ClangUtil::DumpDecl(parser_property_decl.decl)); |
1167 | |
1168 | context.AddNamedDecl(parser_property_decl.decl); |
1169 | found = true; |
1170 | } |
1171 | } |
1172 | |
1173 | DeclFromUser<ObjCIvarDecl> origin_ivar_decl( |
1174 | origin_iface_decl->getIvarDecl(&name_identifier)); |
1175 | |
1176 | if (origin_ivar_decl.IsValid()) { |
1177 | DeclFromParser<ObjCIvarDecl> parser_ivar_decl( |
1178 | origin_ivar_decl.Import(dest_ctx: m_ast_context, importer&: *m_ast_importer_sp)); |
1179 | if (parser_ivar_decl.IsValid()) { |
1180 | LLDB_LOG(log, " CAS::FOPD found\n{0}" , |
1181 | ClangUtil::DumpDecl(parser_ivar_decl.decl)); |
1182 | |
1183 | context.AddNamedDecl(parser_ivar_decl.decl); |
1184 | found = true; |
1185 | } |
1186 | } |
1187 | |
1188 | return found; |
1189 | } |
1190 | |
1191 | void ClangASTSource::FindObjCPropertyAndIvarDecls(NameSearchContext &context) { |
1192 | Log *log = GetLog(mask: LLDBLog::Expressions); |
1193 | |
1194 | DeclFromParser<const ObjCInterfaceDecl> parser_iface_decl( |
1195 | cast<ObjCInterfaceDecl>(Val: context.m_decl_context)); |
1196 | DeclFromUser<const ObjCInterfaceDecl> origin_iface_decl( |
1197 | parser_iface_decl.GetOrigin(importer&: *m_ast_importer_sp)); |
1198 | |
1199 | ConstString class_name(parser_iface_decl->getNameAsString().c_str()); |
1200 | |
1201 | LLDB_LOG(log, |
1202 | "ClangASTSource::FindObjCPropertyAndIvarDecls on " |
1203 | "(ASTContext*){0:x} '{1}' for '{2}.{3}'" , |
1204 | m_ast_context, m_clang_ast_context->getDisplayName(), |
1205 | parser_iface_decl->getName(), context.m_decl_name.getAsString()); |
1206 | |
1207 | if (FindObjCPropertyAndIvarDeclsWithOrigin(context, origin_iface_decl)) |
1208 | return; |
1209 | |
1210 | LLDB_LOG(log, |
1211 | "CAS::FOPD couldn't find the property on origin " |
1212 | "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}, searching " |
1213 | "elsewhere..." , |
1214 | origin_iface_decl.decl, &origin_iface_decl->getASTContext()); |
1215 | |
1216 | SymbolContext null_sc; |
1217 | TypeList type_list; |
1218 | |
1219 | do { |
1220 | ObjCInterfaceDecl *complete_interface_decl = GetCompleteObjCInterface( |
1221 | interface_decl: const_cast<ObjCInterfaceDecl *>(parser_iface_decl.decl)); |
1222 | |
1223 | if (!complete_interface_decl) |
1224 | break; |
1225 | |
1226 | // We found the complete interface. The runtime never needs to be queried |
1227 | // in this scenario. |
1228 | |
1229 | DeclFromUser<const ObjCInterfaceDecl> complete_iface_decl( |
1230 | complete_interface_decl); |
1231 | |
1232 | if (complete_iface_decl.decl == origin_iface_decl.decl) |
1233 | break; // already checked this one |
1234 | |
1235 | LLDB_LOG(log, |
1236 | "CAS::FOPD trying origin " |
1237 | "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}..." , |
1238 | complete_iface_decl.decl, &complete_iface_decl->getASTContext()); |
1239 | |
1240 | FindObjCPropertyAndIvarDeclsWithOrigin(context, origin_iface_decl&: complete_iface_decl); |
1241 | |
1242 | return; |
1243 | } while (false); |
1244 | |
1245 | do { |
1246 | // Check the modules only if the debug information didn't have a complete |
1247 | // interface. |
1248 | |
1249 | std::shared_ptr<ClangModulesDeclVendor> modules_decl_vendor = |
1250 | GetClangModulesDeclVendor(); |
1251 | |
1252 | if (!modules_decl_vendor) |
1253 | break; |
1254 | |
1255 | bool append = false; |
1256 | uint32_t max_matches = 1; |
1257 | std::vector<clang::NamedDecl *> decls; |
1258 | |
1259 | if (!modules_decl_vendor->FindDecls(name: class_name, append, max_matches, decls)) |
1260 | break; |
1261 | |
1262 | DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_modules( |
1263 | dyn_cast<ObjCInterfaceDecl>(Val: decls[0])); |
1264 | |
1265 | if (!interface_decl_from_modules.IsValid()) |
1266 | break; |
1267 | |
1268 | LLDB_LOG(log, |
1269 | "CAS::FOPD[{0:x}] trying module " |
1270 | "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}..." , |
1271 | interface_decl_from_modules.decl, |
1272 | &interface_decl_from_modules->getASTContext()); |
1273 | |
1274 | if (FindObjCPropertyAndIvarDeclsWithOrigin(context, |
1275 | origin_iface_decl&: interface_decl_from_modules)) |
1276 | return; |
1277 | } while (false); |
1278 | |
1279 | do { |
1280 | // Check the runtime only if the debug information didn't have a complete |
1281 | // interface and nothing was in the modules. |
1282 | |
1283 | lldb::ProcessSP process(m_target->GetProcessSP()); |
1284 | |
1285 | if (!process) |
1286 | return; |
1287 | |
1288 | ObjCLanguageRuntime *language_runtime(ObjCLanguageRuntime::Get(process&: *process)); |
1289 | |
1290 | if (!language_runtime) |
1291 | return; |
1292 | |
1293 | DeclVendor *decl_vendor = language_runtime->GetDeclVendor(); |
1294 | |
1295 | if (!decl_vendor) |
1296 | break; |
1297 | |
1298 | bool append = false; |
1299 | uint32_t max_matches = 1; |
1300 | std::vector<clang::NamedDecl *> decls; |
1301 | |
1302 | auto *clang_decl_vendor = llvm::cast<ClangDeclVendor>(Val: decl_vendor); |
1303 | if (!clang_decl_vendor->FindDecls(name: class_name, append, max_matches, decls)) |
1304 | break; |
1305 | |
1306 | DeclFromUser<const ObjCInterfaceDecl> interface_decl_from_runtime( |
1307 | dyn_cast<ObjCInterfaceDecl>(Val: decls[0])); |
1308 | |
1309 | if (!interface_decl_from_runtime.IsValid()) |
1310 | break; |
1311 | |
1312 | LLDB_LOG(log, |
1313 | "CAS::FOPD[{0:x}] trying runtime " |
1314 | "(ObjCInterfaceDecl*){0:x}/(ASTContext*){1:x}..." , |
1315 | interface_decl_from_runtime.decl, |
1316 | &interface_decl_from_runtime->getASTContext()); |
1317 | |
1318 | if (FindObjCPropertyAndIvarDeclsWithOrigin(context, |
1319 | origin_iface_decl&: interface_decl_from_runtime)) |
1320 | return; |
1321 | } while (false); |
1322 | } |
1323 | |
1324 | void ClangASTSource::LookupInNamespace(NameSearchContext &context) { |
1325 | const NamespaceDecl *namespace_context = |
1326 | dyn_cast<NamespaceDecl>(Val: context.m_decl_context); |
1327 | |
1328 | Log *log = GetLog(mask: LLDBLog::Expressions); |
1329 | |
1330 | ClangASTImporter::NamespaceMapSP namespace_map = |
1331 | m_ast_importer_sp->GetNamespaceMap(decl: namespace_context); |
1332 | |
1333 | LLDB_LOGV(log, " CAS::FEVD Inspecting namespace map {0:x} ({1} entries)" , |
1334 | namespace_map.get(), namespace_map->size()); |
1335 | |
1336 | if (!namespace_map) |
1337 | return; |
1338 | |
1339 | for (ClangASTImporter::NamespaceMap::iterator i = namespace_map->begin(), |
1340 | e = namespace_map->end(); |
1341 | i != e; ++i) { |
1342 | LLDB_LOG(log, " CAS::FEVD Searching namespace {0} in module {1}" , |
1343 | i->second.GetName(), i->first->GetFileSpec().GetFilename()); |
1344 | |
1345 | FindExternalVisibleDecls(context, module_sp: i->first, namespace_decl&: i->second); |
1346 | } |
1347 | } |
1348 | |
1349 | bool ClangASTSource::layoutRecordType( |
1350 | const RecordDecl *record, uint64_t &size, uint64_t &alignment, |
1351 | llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets, |
1352 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
1353 | &base_offsets, |
1354 | llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits> |
1355 | &virtual_base_offsets) { |
1356 | return m_ast_importer_sp->importRecordLayoutFromOrigin( |
1357 | record, size, alignment, field_offsets, base_offsets, |
1358 | vbase_offsets&: virtual_base_offsets); |
1359 | } |
1360 | |
1361 | void ClangASTSource::CompleteNamespaceMap( |
1362 | ClangASTImporter::NamespaceMapSP &namespace_map, ConstString name, |
1363 | ClangASTImporter::NamespaceMapSP &parent_map) const { |
1364 | |
1365 | Log *log = GetLog(mask: LLDBLog::Expressions); |
1366 | |
1367 | if (log) { |
1368 | if (parent_map && parent_map->size()) |
1369 | LLDB_LOG(log, |
1370 | "CompleteNamespaceMap on (ASTContext*){0:x} '{1}' Searching " |
1371 | "for namespace {2} in namespace {3}" , |
1372 | m_ast_context, m_clang_ast_context->getDisplayName(), name, |
1373 | parent_map->begin()->second.GetName()); |
1374 | else |
1375 | LLDB_LOG(log, |
1376 | "CompleteNamespaceMap on (ASTContext*){0} '{1}' Searching " |
1377 | "for namespace {2}" , |
1378 | m_ast_context, m_clang_ast_context->getDisplayName(), name); |
1379 | } |
1380 | |
1381 | if (parent_map) { |
1382 | for (ClangASTImporter::NamespaceMap::iterator i = parent_map->begin(), |
1383 | e = parent_map->end(); |
1384 | i != e; ++i) { |
1385 | CompilerDeclContext found_namespace_decl; |
1386 | |
1387 | lldb::ModuleSP module_sp = i->first; |
1388 | CompilerDeclContext module_parent_namespace_decl = i->second; |
1389 | |
1390 | SymbolFile *symbol_file = module_sp->GetSymbolFile(); |
1391 | |
1392 | if (!symbol_file) |
1393 | continue; |
1394 | |
1395 | found_namespace_decl = |
1396 | symbol_file->FindNamespace(name, parent_decl_ctx: module_parent_namespace_decl); |
1397 | |
1398 | if (!found_namespace_decl) |
1399 | continue; |
1400 | |
1401 | namespace_map->push_back(x: std::pair<lldb::ModuleSP, CompilerDeclContext>( |
1402 | module_sp, found_namespace_decl)); |
1403 | |
1404 | LLDB_LOG(log, " CMN Found namespace {0} in module {1}" , name, |
1405 | module_sp->GetFileSpec().GetFilename()); |
1406 | } |
1407 | } else { |
1408 | CompilerDeclContext null_namespace_decl; |
1409 | for (lldb::ModuleSP image : m_target->GetImages().Modules()) { |
1410 | if (!image) |
1411 | continue; |
1412 | |
1413 | CompilerDeclContext found_namespace_decl; |
1414 | |
1415 | SymbolFile *symbol_file = image->GetSymbolFile(); |
1416 | |
1417 | if (!symbol_file) |
1418 | continue; |
1419 | |
1420 | found_namespace_decl = |
1421 | symbol_file->FindNamespace(name, parent_decl_ctx: null_namespace_decl); |
1422 | |
1423 | if (!found_namespace_decl) |
1424 | continue; |
1425 | |
1426 | namespace_map->push_back(x: std::pair<lldb::ModuleSP, CompilerDeclContext>( |
1427 | image, found_namespace_decl)); |
1428 | |
1429 | LLDB_LOG(log, " CMN[{0}] Found namespace {0} in module {1}" , name, |
1430 | image->GetFileSpec().GetFilename()); |
1431 | } |
1432 | } |
1433 | } |
1434 | |
1435 | NamespaceDecl *ClangASTSource::AddNamespace( |
1436 | NameSearchContext &context, |
1437 | ClangASTImporter::NamespaceMapSP &namespace_decls) { |
1438 | if (!namespace_decls) |
1439 | return nullptr; |
1440 | |
1441 | const CompilerDeclContext &namespace_decl = namespace_decls->begin()->second; |
1442 | |
1443 | clang::ASTContext *src_ast = |
1444 | TypeSystemClang::DeclContextGetTypeSystemClang(dc: namespace_decl); |
1445 | if (!src_ast) |
1446 | return nullptr; |
1447 | clang::NamespaceDecl *src_namespace_decl = |
1448 | TypeSystemClang::DeclContextGetAsNamespaceDecl(dc: namespace_decl); |
1449 | |
1450 | if (!src_namespace_decl) |
1451 | return nullptr; |
1452 | |
1453 | Decl *copied_decl = CopyDecl(src_namespace_decl); |
1454 | |
1455 | if (!copied_decl) |
1456 | return nullptr; |
1457 | |
1458 | NamespaceDecl *copied_namespace_decl = dyn_cast<NamespaceDecl>(Val: copied_decl); |
1459 | |
1460 | if (!copied_namespace_decl) |
1461 | return nullptr; |
1462 | |
1463 | context.m_decls.push_back(copied_namespace_decl); |
1464 | |
1465 | m_ast_importer_sp->RegisterNamespaceMap(decl: copied_namespace_decl, |
1466 | namespace_map&: namespace_decls); |
1467 | |
1468 | return dyn_cast<NamespaceDecl>(Val: copied_decl); |
1469 | } |
1470 | |
1471 | clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) { |
1472 | return m_ast_importer_sp->CopyDecl(dst_ctx: m_ast_context, decl: src_decl); |
1473 | } |
1474 | |
1475 | ClangASTImporter::DeclOrigin ClangASTSource::GetDeclOrigin(const clang::Decl *decl) { |
1476 | return m_ast_importer_sp->GetDeclOrigin(decl); |
1477 | } |
1478 | |
1479 | CompilerType ClangASTSource::GuardedCopyType(const CompilerType &src_type) { |
1480 | auto src_ast = src_type.GetTypeSystem<TypeSystemClang>(); |
1481 | if (!src_ast) |
1482 | return {}; |
1483 | |
1484 | QualType copied_qual_type = ClangUtil::GetQualType( |
1485 | ct: m_ast_importer_sp->CopyType(dst&: *m_clang_ast_context, src_type)); |
1486 | |
1487 | if (copied_qual_type.getAsOpaquePtr() && |
1488 | copied_qual_type->getCanonicalTypeInternal().isNull()) |
1489 | // this shouldn't happen, but we're hardening because the AST importer |
1490 | // seems to be generating bad types on occasion. |
1491 | return {}; |
1492 | |
1493 | return m_clang_ast_context->GetType(qt: copied_qual_type); |
1494 | } |
1495 | |
1496 | std::shared_ptr<ClangModulesDeclVendor> |
1497 | ClangASTSource::GetClangModulesDeclVendor() { |
1498 | auto persistent_vars = llvm::cast<ClangPersistentVariables>( |
1499 | Val: m_target->GetPersistentExpressionStateForLanguage(language: lldb::eLanguageTypeC)); |
1500 | return persistent_vars->GetClangModulesDeclVendor(); |
1501 | } |
1502 | |