]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/lldb/source/Symbol/ClangASTImporter.cpp
Fix a memory leak in if_delgroups() introduced in r334118.
[FreeBSD/FreeBSD.git] / contrib / llvm-project / lldb / source / Symbol / ClangASTImporter.cpp
1 //===-- ClangASTImporter.cpp ------------------------------------*- C++ -*-===//
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 "lldb/Symbol/ClangASTImporter.h"
10 #include "lldb/Core/Module.h"
11 #include "lldb/Symbol/ClangASTContext.h"
12 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
13 #include "lldb/Symbol/ClangUtil.h"
14 #include "lldb/Utility/LLDBAssert.h"
15 #include "lldb/Utility/Log.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclObjC.h"
19 #include "clang/Sema/Lookup.h"
20 #include "clang/Sema/Sema.h"
21 #include "llvm/Support/raw_ostream.h"
22
23 #include <memory>
24
25 using namespace lldb_private;
26 using namespace clang;
27
28 ClangASTMetrics::Counters ClangASTMetrics::global_counters = {0, 0, 0, 0, 0, 0};
29 ClangASTMetrics::Counters ClangASTMetrics::local_counters = {0, 0, 0, 0, 0, 0};
30
31 void ClangASTMetrics::DumpCounters(Log *log,
32                                    ClangASTMetrics::Counters &counters) {
33   log->Printf("  Number of visible Decl queries by name     : %" PRIu64,
34               counters.m_visible_query_count);
35   log->Printf("  Number of lexical Decl queries             : %" PRIu64,
36               counters.m_lexical_query_count);
37   log->Printf("  Number of imports initiated by LLDB        : %" PRIu64,
38               counters.m_lldb_import_count);
39   log->Printf("  Number of imports conducted by Clang       : %" PRIu64,
40               counters.m_clang_import_count);
41   log->Printf("  Number of Decls completed                  : %" PRIu64,
42               counters.m_decls_completed_count);
43   log->Printf("  Number of records laid out                 : %" PRIu64,
44               counters.m_record_layout_count);
45 }
46
47 void ClangASTMetrics::DumpCounters(Log *log) {
48   if (!log)
49     return;
50
51   log->Printf("== ClangASTMetrics output ==");
52   log->Printf("-- Global metrics --");
53   DumpCounters(log, global_counters);
54   log->Printf("-- Local metrics --");
55   DumpCounters(log, local_counters);
56 }
57
58 clang::QualType ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
59                                            clang::ASTContext *src_ast,
60                                            clang::QualType type) {
61   ImporterDelegateSP delegate_sp(GetDelegate(dst_ast, src_ast));
62
63   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);
64
65   if (!delegate_sp)
66     return QualType();
67
68   llvm::Expected<QualType> ret_or_error = delegate_sp->Import(type);
69   if (!ret_or_error) {
70     Log *log =
71       lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
72     LLDB_LOG_ERROR(log, ret_or_error.takeError(),
73         "Couldn't import type: {0}");
74     return QualType();
75   }
76   return *ret_or_error;
77 }
78
79 lldb::opaque_compiler_type_t
80 ClangASTImporter::CopyType(clang::ASTContext *dst_ast,
81                            clang::ASTContext *src_ast,
82                            lldb::opaque_compiler_type_t type) {
83   return CopyType(dst_ast, src_ast, QualType::getFromOpaquePtr(type))
84       .getAsOpaquePtr();
85 }
86
87 CompilerType ClangASTImporter::CopyType(ClangASTContext &dst_ast,
88                                         const CompilerType &src_type) {
89   clang::ASTContext *dst_clang_ast = dst_ast.getASTContext();
90   if (dst_clang_ast) {
91     ClangASTContext *src_ast =
92         llvm::dyn_cast_or_null<ClangASTContext>(src_type.GetTypeSystem());
93     if (src_ast) {
94       clang::ASTContext *src_clang_ast = src_ast->getASTContext();
95       if (src_clang_ast) {
96         lldb::opaque_compiler_type_t dst_clang_type = CopyType(
97             dst_clang_ast, src_clang_ast, src_type.GetOpaqueQualType());
98
99         if (dst_clang_type)
100           return CompilerType(&dst_ast, dst_clang_type);
101       }
102     }
103   }
104   return CompilerType();
105 }
106
107 clang::Decl *ClangASTImporter::CopyDecl(clang::ASTContext *dst_ast,
108                                         clang::ASTContext *src_ast,
109                                         clang::Decl *decl) {
110   ImporterDelegateSP delegate_sp;
111
112   delegate_sp = GetDelegate(dst_ast, src_ast);
113
114   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);
115
116   if (!delegate_sp)
117     return nullptr;
118
119   llvm::Expected<clang::Decl *> result = delegate_sp->Import(decl);
120   if (!result) {
121     Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
122     LLDB_LOG_ERROR(log, result.takeError(), "Couldn't import decl: {0}");
123     if (log) {
124       lldb::user_id_t user_id = LLDB_INVALID_UID;
125       ClangASTMetadata *metadata = GetDeclMetadata(decl);
126       if (metadata)
127         user_id = metadata->GetUserID();
128
129       if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
130         log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s "
131                     "'%s', metadata 0x%" PRIx64,
132                     decl->getDeclKindName(),
133                     named_decl->getNameAsString().c_str(), user_id);
134       else
135         log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s, "
136                     "metadata 0x%" PRIx64,
137                     decl->getDeclKindName(), user_id);
138     }
139     return nullptr;
140   }
141
142   return *result;
143 }
144
145 class DeclContextOverride {
146 private:
147   struct Backup {
148     clang::DeclContext *decl_context;
149     clang::DeclContext *lexical_decl_context;
150   };
151
152   std::map<clang::Decl *, Backup> m_backups;
153
154   void OverrideOne(clang::Decl *decl) {
155     if (m_backups.find(decl) != m_backups.end()) {
156       return;
157     }
158
159     m_backups[decl] = {decl->getDeclContext(), decl->getLexicalDeclContext()};
160
161     decl->setDeclContext(decl->getASTContext().getTranslationUnitDecl());
162     decl->setLexicalDeclContext(decl->getASTContext().getTranslationUnitDecl());
163   }
164
165   bool ChainPassesThrough(
166       clang::Decl *decl, clang::DeclContext *base,
167       clang::DeclContext *(clang::Decl::*contextFromDecl)(),
168       clang::DeclContext *(clang::DeclContext::*contextFromContext)()) {
169     for (DeclContext *decl_ctx = (decl->*contextFromDecl)(); decl_ctx;
170          decl_ctx = (decl_ctx->*contextFromContext)()) {
171       if (decl_ctx == base) {
172         return true;
173       }
174     }
175
176     return false;
177   }
178
179   clang::Decl *GetEscapedChild(clang::Decl *decl,
180                                clang::DeclContext *base = nullptr) {
181     if (base) {
182       // decl's DeclContext chains must pass through base.
183
184       if (!ChainPassesThrough(decl, base, &clang::Decl::getDeclContext,
185                               &clang::DeclContext::getParent) ||
186           !ChainPassesThrough(decl, base, &clang::Decl::getLexicalDeclContext,
187                               &clang::DeclContext::getLexicalParent)) {
188         return decl;
189       }
190     } else {
191       base = clang::dyn_cast<clang::DeclContext>(decl);
192
193       if (!base) {
194         return nullptr;
195       }
196     }
197
198     if (clang::DeclContext *context =
199             clang::dyn_cast<clang::DeclContext>(decl)) {
200       for (clang::Decl *decl : context->decls()) {
201         if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
202           return escaped_child;
203         }
204       }
205     }
206
207     return nullptr;
208   }
209
210   void Override(clang::Decl *decl) {
211     if (clang::Decl *escaped_child = GetEscapedChild(decl)) {
212       Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
213
214       if (log)
215         log->Printf("    [ClangASTImporter] DeclContextOverride couldn't "
216                     "override (%sDecl*)%p - its child (%sDecl*)%p escapes",
217                     decl->getDeclKindName(), static_cast<void *>(decl),
218                     escaped_child->getDeclKindName(),
219                     static_cast<void *>(escaped_child));
220       lldbassert(0 && "Couldn't override!");
221     }
222
223     OverrideOne(decl);
224   }
225
226 public:
227   DeclContextOverride() {}
228
229   void OverrideAllDeclsFromContainingFunction(clang::Decl *decl) {
230     for (DeclContext *decl_context = decl->getLexicalDeclContext();
231          decl_context; decl_context = decl_context->getLexicalParent()) {
232       DeclContext *redecl_context = decl_context->getRedeclContext();
233
234       if (llvm::isa<FunctionDecl>(redecl_context) &&
235           llvm::isa<TranslationUnitDecl>(redecl_context->getLexicalParent())) {
236         for (clang::Decl *child_decl : decl_context->decls()) {
237           Override(child_decl);
238         }
239       }
240     }
241   }
242
243   ~DeclContextOverride() {
244     for (const std::pair<clang::Decl *, Backup> &backup : m_backups) {
245       backup.first->setDeclContext(backup.second.decl_context);
246       backup.first->setLexicalDeclContext(backup.second.lexical_decl_context);
247     }
248   }
249 };
250
251 lldb::opaque_compiler_type_t
252 ClangASTImporter::DeportType(clang::ASTContext *dst_ctx,
253                              clang::ASTContext *src_ctx,
254                              lldb::opaque_compiler_type_t type) {
255   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
256
257   if (log)
258     log->Printf("    [ClangASTImporter] DeportType called on (%sType*)0x%llx "
259                 "from (ASTContext*)%p to (ASTContext*)%p",
260                 QualType::getFromOpaquePtr(type)->getTypeClassName(),
261                 (unsigned long long)type, static_cast<void *>(src_ctx),
262                 static_cast<void *>(dst_ctx));
263
264   ImporterDelegateSP delegate_sp(GetDelegate(dst_ctx, src_ctx));
265
266   if (!delegate_sp)
267     return nullptr;
268
269   std::set<NamedDecl *> decls_to_deport;
270   std::set<NamedDecl *> decls_already_deported;
271
272   DeclContextOverride decl_context_override;
273
274   if (const clang::TagType *tag_type =
275           clang::QualType::getFromOpaquePtr(type)->getAs<TagType>()) {
276     decl_context_override.OverrideAllDeclsFromContainingFunction(
277         tag_type->getDecl());
278   }
279
280   delegate_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
281
282   lldb::opaque_compiler_type_t result = CopyType(dst_ctx, src_ctx, type);
283
284   delegate_sp->ExecuteDeportWorkQueues();
285
286   if (!result)
287     return nullptr;
288
289   return result;
290 }
291
292 clang::Decl *ClangASTImporter::DeportDecl(clang::ASTContext *dst_ctx,
293                                           clang::ASTContext *src_ctx,
294                                           clang::Decl *decl) {
295   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
296
297   if (log)
298     log->Printf("    [ClangASTImporter] DeportDecl called on (%sDecl*)%p from "
299                 "(ASTContext*)%p to (ASTContext*)%p",
300                 decl->getDeclKindName(), static_cast<void *>(decl),
301                 static_cast<void *>(src_ctx), static_cast<void *>(dst_ctx));
302
303   ImporterDelegateSP delegate_sp(GetDelegate(dst_ctx, src_ctx));
304
305   if (!delegate_sp)
306     return nullptr;
307
308   std::set<NamedDecl *> decls_to_deport;
309   std::set<NamedDecl *> decls_already_deported;
310
311   DeclContextOverride decl_context_override;
312
313   decl_context_override.OverrideAllDeclsFromContainingFunction(decl);
314
315   delegate_sp->InitDeportWorkQueues(&decls_to_deport, &decls_already_deported);
316
317   clang::Decl *result = CopyDecl(dst_ctx, src_ctx, decl);
318
319   delegate_sp->ExecuteDeportWorkQueues();
320
321   if (!result)
322     return nullptr;
323
324   if (log)
325     log->Printf(
326         "    [ClangASTImporter] DeportDecl deported (%sDecl*)%p to (%sDecl*)%p",
327         decl->getDeclKindName(), static_cast<void *>(decl),
328         result->getDeclKindName(), static_cast<void *>(result));
329
330   return result;
331 }
332
333 bool ClangASTImporter::CanImport(const CompilerType &type) {
334   if (!ClangUtil::IsClangType(type))
335     return false;
336
337   // TODO: remove external completion BOOL
338   // CompleteAndFetchChildren should get the Decl out and check for the
339
340   clang::QualType qual_type(
341       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
342
343   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
344   switch (type_class) {
345   case clang::Type::Record: {
346     const clang::CXXRecordDecl *cxx_record_decl =
347         qual_type->getAsCXXRecordDecl();
348     if (cxx_record_decl) {
349       if (ResolveDeclOrigin(cxx_record_decl, nullptr, nullptr))
350         return true;
351     }
352   } break;
353
354   case clang::Type::Enum: {
355     clang::EnumDecl *enum_decl =
356         llvm::cast<clang::EnumType>(qual_type)->getDecl();
357     if (enum_decl) {
358       if (ResolveDeclOrigin(enum_decl, nullptr, nullptr))
359         return true;
360     }
361   } break;
362
363   case clang::Type::ObjCObject:
364   case clang::Type::ObjCInterface: {
365     const clang::ObjCObjectType *objc_class_type =
366         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
367     if (objc_class_type) {
368       clang::ObjCInterfaceDecl *class_interface_decl =
369           objc_class_type->getInterface();
370       // We currently can't complete objective C types through the newly added
371       // ASTContext because it only supports TagDecl objects right now...
372       if (class_interface_decl) {
373         if (ResolveDeclOrigin(class_interface_decl, nullptr, nullptr))
374           return true;
375       }
376     }
377   } break;
378
379   case clang::Type::Typedef:
380     return CanImport(CompilerType(type.GetTypeSystem(),
381                                   llvm::cast<clang::TypedefType>(qual_type)
382                                       ->getDecl()
383                                       ->getUnderlyingType()
384                                       .getAsOpaquePtr()));
385
386   case clang::Type::Auto:
387     return CanImport(CompilerType(type.GetTypeSystem(),
388                                   llvm::cast<clang::AutoType>(qual_type)
389                                       ->getDeducedType()
390                                       .getAsOpaquePtr()));
391
392   case clang::Type::Elaborated:
393     return CanImport(CompilerType(type.GetTypeSystem(),
394                                   llvm::cast<clang::ElaboratedType>(qual_type)
395                                       ->getNamedType()
396                                       .getAsOpaquePtr()));
397
398   case clang::Type::Paren:
399     return CanImport(CompilerType(
400         type.GetTypeSystem(),
401         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
402
403   default:
404     break;
405   }
406
407   return false;
408 }
409
410 bool ClangASTImporter::Import(const CompilerType &type) {
411   if (!ClangUtil::IsClangType(type))
412     return false;
413   // TODO: remove external completion BOOL
414   // CompleteAndFetchChildren should get the Decl out and check for the
415
416   clang::QualType qual_type(
417       ClangUtil::GetCanonicalQualType(ClangUtil::RemoveFastQualifiers(type)));
418
419   const clang::Type::TypeClass type_class = qual_type->getTypeClass();
420   switch (type_class) {
421   case clang::Type::Record: {
422     const clang::CXXRecordDecl *cxx_record_decl =
423         qual_type->getAsCXXRecordDecl();
424     if (cxx_record_decl) {
425       if (ResolveDeclOrigin(cxx_record_decl, nullptr, nullptr))
426         return CompleteAndFetchChildren(qual_type);
427     }
428   } break;
429
430   case clang::Type::Enum: {
431     clang::EnumDecl *enum_decl =
432         llvm::cast<clang::EnumType>(qual_type)->getDecl();
433     if (enum_decl) {
434       if (ResolveDeclOrigin(enum_decl, nullptr, nullptr))
435         return CompleteAndFetchChildren(qual_type);
436     }
437   } break;
438
439   case clang::Type::ObjCObject:
440   case clang::Type::ObjCInterface: {
441     const clang::ObjCObjectType *objc_class_type =
442         llvm::dyn_cast<clang::ObjCObjectType>(qual_type);
443     if (objc_class_type) {
444       clang::ObjCInterfaceDecl *class_interface_decl =
445           objc_class_type->getInterface();
446       // We currently can't complete objective C types through the newly added
447       // ASTContext because it only supports TagDecl objects right now...
448       if (class_interface_decl) {
449         if (ResolveDeclOrigin(class_interface_decl, nullptr, nullptr))
450           return CompleteAndFetchChildren(qual_type);
451       }
452     }
453   } break;
454
455   case clang::Type::Typedef:
456     return Import(CompilerType(type.GetTypeSystem(),
457                                llvm::cast<clang::TypedefType>(qual_type)
458                                    ->getDecl()
459                                    ->getUnderlyingType()
460                                    .getAsOpaquePtr()));
461
462   case clang::Type::Auto:
463     return Import(CompilerType(type.GetTypeSystem(),
464                                llvm::cast<clang::AutoType>(qual_type)
465                                    ->getDeducedType()
466                                    .getAsOpaquePtr()));
467
468   case clang::Type::Elaborated:
469     return Import(CompilerType(type.GetTypeSystem(),
470                                llvm::cast<clang::ElaboratedType>(qual_type)
471                                    ->getNamedType()
472                                    .getAsOpaquePtr()));
473
474   case clang::Type::Paren:
475     return Import(CompilerType(
476         type.GetTypeSystem(),
477         llvm::cast<clang::ParenType>(qual_type)->desugar().getAsOpaquePtr()));
478
479   default:
480     break;
481   }
482   return false;
483 }
484
485 bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
486   if (!CanImport(compiler_type))
487     return false;
488
489   if (Import(compiler_type)) {
490     ClangASTContext::CompleteTagDeclarationDefinition(compiler_type);
491     return true;
492   }
493
494   ClangASTContext::SetHasExternalStorage(compiler_type.GetOpaqueQualType(),
495                                          false);
496   return false;
497 }
498
499 bool ClangASTImporter::LayoutRecordType(
500     const clang::RecordDecl *record_decl, uint64_t &bit_size,
501     uint64_t &alignment,
502     llvm::DenseMap<const clang::FieldDecl *, uint64_t> &field_offsets,
503     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
504         &base_offsets,
505     llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>
506         &vbase_offsets) {
507   RecordDeclToLayoutMap::iterator pos =
508       m_record_decl_to_layout_map.find(record_decl);
509   bool success = false;
510   base_offsets.clear();
511   vbase_offsets.clear();
512   if (pos != m_record_decl_to_layout_map.end()) {
513     bit_size = pos->second.bit_size;
514     alignment = pos->second.alignment;
515     field_offsets.swap(pos->second.field_offsets);
516     base_offsets.swap(pos->second.base_offsets);
517     vbase_offsets.swap(pos->second.vbase_offsets);
518     m_record_decl_to_layout_map.erase(pos);
519     success = true;
520   } else {
521     bit_size = 0;
522     alignment = 0;
523     field_offsets.clear();
524   }
525   return success;
526 }
527
528 void ClangASTImporter::InsertRecordDecl(clang::RecordDecl *decl,
529                                         const LayoutInfo &layout) {
530   m_record_decl_to_layout_map.insert(std::make_pair(decl, layout));
531 }
532
533 void ClangASTImporter::CompleteDecl(clang::Decl *decl) {
534   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
535
536   if (log)
537     log->Printf("    [ClangASTImporter] CompleteDecl called on (%sDecl*)%p",
538                 decl->getDeclKindName(), static_cast<void *>(decl));
539
540   if (ObjCInterfaceDecl *interface_decl = dyn_cast<ObjCInterfaceDecl>(decl)) {
541     if (!interface_decl->getDefinition()) {
542       interface_decl->startDefinition();
543       CompleteObjCInterfaceDecl(interface_decl);
544     }
545   } else if (ObjCProtocolDecl *protocol_decl =
546                  dyn_cast<ObjCProtocolDecl>(decl)) {
547     if (!protocol_decl->getDefinition())
548       protocol_decl->startDefinition();
549   } else if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
550     if (!tag_decl->getDefinition() && !tag_decl->isBeingDefined()) {
551       tag_decl->startDefinition();
552       CompleteTagDecl(tag_decl);
553       tag_decl->setCompleteDefinition(true);
554     }
555   } else {
556     assert(0 && "CompleteDecl called on a Decl that can't be completed");
557   }
558 }
559
560 bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
561   ClangASTMetrics::RegisterDeclCompletion();
562
563   DeclOrigin decl_origin = GetDeclOrigin(decl);
564
565   if (!decl_origin.Valid())
566     return false;
567
568   if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
569     return false;
570
571   ImporterDelegateSP delegate_sp(
572       GetDelegate(&decl->getASTContext(), decl_origin.ctx));
573
574   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
575                                                 &decl->getASTContext());
576   if (delegate_sp)
577     delegate_sp->ImportDefinitionTo(decl, decl_origin.decl);
578
579   return true;
580 }
581
582 bool ClangASTImporter::CompleteTagDeclWithOrigin(clang::TagDecl *decl,
583                                                  clang::TagDecl *origin_decl) {
584   ClangASTMetrics::RegisterDeclCompletion();
585
586   clang::ASTContext *origin_ast_ctx = &origin_decl->getASTContext();
587
588   if (!ClangASTContext::GetCompleteDecl(origin_ast_ctx, origin_decl))
589     return false;
590
591   ImporterDelegateSP delegate_sp(
592       GetDelegate(&decl->getASTContext(), origin_ast_ctx));
593
594   if (delegate_sp)
595     delegate_sp->ImportDefinitionTo(decl, origin_decl);
596
597   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
598
599   OriginMap &origins = context_md->m_origins;
600
601   origins[decl] = DeclOrigin(origin_ast_ctx, origin_decl);
602
603   return true;
604 }
605
606 bool ClangASTImporter::CompleteObjCInterfaceDecl(
607     clang::ObjCInterfaceDecl *interface_decl) {
608   ClangASTMetrics::RegisterDeclCompletion();
609
610   DeclOrigin decl_origin = GetDeclOrigin(interface_decl);
611
612   if (!decl_origin.Valid())
613     return false;
614
615   if (!ClangASTContext::GetCompleteDecl(decl_origin.ctx, decl_origin.decl))
616     return false;
617
618   ImporterDelegateSP delegate_sp(
619       GetDelegate(&interface_decl->getASTContext(), decl_origin.ctx));
620
621   if (delegate_sp)
622     delegate_sp->ImportDefinitionTo(interface_decl, decl_origin.decl);
623
624   if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass())
625     RequireCompleteType(clang::QualType(super_class->getTypeForDecl(), 0));
626
627   return true;
628 }
629
630 bool ClangASTImporter::CompleteAndFetchChildren(clang::QualType type) {
631   if (!RequireCompleteType(type))
632     return false;
633
634   Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
635
636   if (const TagType *tag_type = type->getAs<TagType>()) {
637     TagDecl *tag_decl = tag_type->getDecl();
638
639     DeclOrigin decl_origin = GetDeclOrigin(tag_decl);
640
641     if (!decl_origin.Valid())
642       return false;
643
644     ImporterDelegateSP delegate_sp(
645         GetDelegate(&tag_decl->getASTContext(), decl_origin.ctx));
646
647     ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp,
648                                                   &tag_decl->getASTContext());
649
650     TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
651
652     for (Decl *origin_child_decl : origin_tag_decl->decls()) {
653       llvm::Expected<Decl *> imported_or_err =
654           delegate_sp->Import(origin_child_decl);
655       if (!imported_or_err) {
656         LLDB_LOG_ERROR(log, imported_or_err.takeError(),
657                        "Couldn't import decl: {0}");
658         return false;
659       }
660     }
661
662     if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl)) {
663       record_decl->setHasLoadedFieldsFromExternalStorage(true);
664     }
665
666     return true;
667   }
668
669   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
670     if (ObjCInterfaceDecl *objc_interface_decl =
671             objc_object_type->getInterface()) {
672       DeclOrigin decl_origin = GetDeclOrigin(objc_interface_decl);
673
674       if (!decl_origin.Valid())
675         return false;
676
677       ImporterDelegateSP delegate_sp(
678           GetDelegate(&objc_interface_decl->getASTContext(), decl_origin.ctx));
679
680       ObjCInterfaceDecl *origin_interface_decl =
681           llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
682
683       for (Decl *origin_child_decl : origin_interface_decl->decls()) {
684         llvm::Expected<Decl *> imported_or_err =
685             delegate_sp->Import(origin_child_decl);
686         if (!imported_or_err) {
687           LLDB_LOG_ERROR(log, imported_or_err.takeError(),
688                          "Couldn't import decl: {0}");
689           return false;
690         }
691       }
692
693       return true;
694     } else {
695       return false;
696     }
697   }
698
699   return true;
700 }
701
702 bool ClangASTImporter::RequireCompleteType(clang::QualType type) {
703   if (type.isNull())
704     return false;
705
706   if (const TagType *tag_type = type->getAs<TagType>()) {
707     TagDecl *tag_decl = tag_type->getDecl();
708
709     if (tag_decl->getDefinition() || tag_decl->isBeingDefined())
710       return true;
711
712     return CompleteTagDecl(tag_decl);
713   }
714   if (const ObjCObjectType *objc_object_type = type->getAs<ObjCObjectType>()) {
715     if (ObjCInterfaceDecl *objc_interface_decl =
716             objc_object_type->getInterface())
717       return CompleteObjCInterfaceDecl(objc_interface_decl);
718     else
719       return false;
720   }
721   if (const ArrayType *array_type = type->getAsArrayTypeUnsafe()) {
722     return RequireCompleteType(array_type->getElementType());
723   }
724   if (const AtomicType *atomic_type = type->getAs<AtomicType>()) {
725     return RequireCompleteType(atomic_type->getPointeeType());
726   }
727
728   return true;
729 }
730
731 ClangASTMetadata *ClangASTImporter::GetDeclMetadata(const clang::Decl *decl) {
732   DeclOrigin decl_origin = GetDeclOrigin(decl);
733
734   if (decl_origin.Valid())
735     return ClangASTContext::GetMetadata(decl_origin.ctx, decl_origin.decl);
736   else
737     return ClangASTContext::GetMetadata(&decl->getASTContext(), decl);
738 }
739
740 ClangASTImporter::DeclOrigin
741 ClangASTImporter::GetDeclOrigin(const clang::Decl *decl) {
742   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
743
744   OriginMap &origins = context_md->m_origins;
745
746   OriginMap::iterator iter = origins.find(decl);
747
748   if (iter != origins.end())
749     return iter->second;
750   else
751     return DeclOrigin();
752 }
753
754 void ClangASTImporter::SetDeclOrigin(const clang::Decl *decl,
755                                      clang::Decl *original_decl) {
756   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
757
758   OriginMap &origins = context_md->m_origins;
759
760   OriginMap::iterator iter = origins.find(decl);
761
762   if (iter != origins.end()) {
763     iter->second.decl = original_decl;
764     iter->second.ctx = &original_decl->getASTContext();
765   } else {
766     origins[decl] = DeclOrigin(&original_decl->getASTContext(), original_decl);
767   }
768 }
769
770 void ClangASTImporter::RegisterNamespaceMap(const clang::NamespaceDecl *decl,
771                                             NamespaceMapSP &namespace_map) {
772   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
773
774   context_md->m_namespace_maps[decl] = namespace_map;
775 }
776
777 ClangASTImporter::NamespaceMapSP
778 ClangASTImporter::GetNamespaceMap(const clang::NamespaceDecl *decl) {
779   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
780
781   NamespaceMetaMap &namespace_maps = context_md->m_namespace_maps;
782
783   NamespaceMetaMap::iterator iter = namespace_maps.find(decl);
784
785   if (iter != namespace_maps.end())
786     return iter->second;
787   else
788     return NamespaceMapSP();
789 }
790
791 void ClangASTImporter::BuildNamespaceMap(const clang::NamespaceDecl *decl) {
792   assert(decl);
793   ASTContextMetadataSP context_md = GetContextMetadata(&decl->getASTContext());
794
795   const DeclContext *parent_context = decl->getDeclContext();
796   const NamespaceDecl *parent_namespace =
797       dyn_cast<NamespaceDecl>(parent_context);
798   NamespaceMapSP parent_map;
799
800   if (parent_namespace)
801     parent_map = GetNamespaceMap(parent_namespace);
802
803   NamespaceMapSP new_map;
804
805   new_map = std::make_shared<NamespaceMap>();
806
807   if (context_md->m_map_completer) {
808     std::string namespace_string = decl->getDeclName().getAsString();
809
810     context_md->m_map_completer->CompleteNamespaceMap(
811         new_map, ConstString(namespace_string.c_str()), parent_map);
812   }
813
814   context_md->m_namespace_maps[decl] = new_map;
815 }
816
817 void ClangASTImporter::ForgetDestination(clang::ASTContext *dst_ast) {
818   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
819
820   if (log)
821     log->Printf("    [ClangASTImporter] Forgetting destination (ASTContext*)%p",
822                 static_cast<void *>(dst_ast));
823
824   m_metadata_map.erase(dst_ast);
825 }
826
827 void ClangASTImporter::ForgetSource(clang::ASTContext *dst_ast,
828                                     clang::ASTContext *src_ast) {
829   ASTContextMetadataSP md = MaybeGetContextMetadata(dst_ast);
830
831   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
832
833   if (log)
834     log->Printf("    [ClangASTImporter] Forgetting source->dest "
835                 "(ASTContext*)%p->(ASTContext*)%p",
836                 static_cast<void *>(src_ast), static_cast<void *>(dst_ast));
837
838   if (!md)
839     return;
840
841   md->m_delegates.erase(src_ast);
842
843   for (OriginMap::iterator iter = md->m_origins.begin();
844        iter != md->m_origins.end();) {
845     if (iter->second.ctx == src_ast)
846       md->m_origins.erase(iter++);
847     else
848       ++iter;
849   }
850 }
851
852 ClangASTImporter::MapCompleter::~MapCompleter() { return; }
853
854 llvm::Expected<Decl *>
855 ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
856   if (m_std_handler) {
857     llvm::Optional<Decl *> D = m_std_handler->Import(From);
858     if (D) {
859       // Make sure we don't use this decl later to map it back to it's original
860       // decl. The decl the CxxModuleHandler created has nothing to do with
861       // the one from debug info, and linking those two would just cause the
862       // ASTImporter to try 'updating' the module decl with the minimal one from
863       // the debug info.
864       m_decls_to_ignore.insert(*D);
865       return *D;
866     }
867   }
868
869   return ASTImporter::ImportImpl(From);
870 }
871
872 void ClangASTImporter::ASTImporterDelegate::InitDeportWorkQueues(
873     std::set<clang::NamedDecl *> *decls_to_deport,
874     std::set<clang::NamedDecl *> *decls_already_deported) {
875   assert(!m_decls_to_deport);
876   assert(!m_decls_already_deported);
877
878   m_decls_to_deport = decls_to_deport;
879   m_decls_already_deported = decls_already_deported;
880 }
881
882 void ClangASTImporter::ASTImporterDelegate::ExecuteDeportWorkQueues() {
883   assert(m_decls_to_deport);
884   assert(m_decls_already_deported);
885
886   ASTContextMetadataSP to_context_md =
887       m_master.GetContextMetadata(&getToContext());
888
889   while (!m_decls_to_deport->empty()) {
890     NamedDecl *decl = *m_decls_to_deport->begin();
891
892     m_decls_already_deported->insert(decl);
893     m_decls_to_deport->erase(decl);
894
895     DeclOrigin &origin = to_context_md->m_origins[decl];
896     UNUSED_IF_ASSERT_DISABLED(origin);
897
898     assert(origin.ctx ==
899            m_source_ctx); // otherwise we should never have added this
900                           // because it doesn't need to be deported
901
902     Decl *original_decl = to_context_md->m_origins[decl].decl;
903
904     ClangASTContext::GetCompleteDecl(m_source_ctx, original_decl);
905
906     if (TagDecl *tag_decl = dyn_cast<TagDecl>(decl)) {
907       if (TagDecl *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
908         if (original_tag_decl->isCompleteDefinition()) {
909           ImportDefinitionTo(tag_decl, original_tag_decl);
910           tag_decl->setCompleteDefinition(true);
911         }
912       }
913
914       tag_decl->setHasExternalLexicalStorage(false);
915       tag_decl->setHasExternalVisibleStorage(false);
916     } else if (ObjCContainerDecl *container_decl =
917                    dyn_cast<ObjCContainerDecl>(decl)) {
918       container_decl->setHasExternalLexicalStorage(false);
919       container_decl->setHasExternalVisibleStorage(false);
920     }
921
922     to_context_md->m_origins.erase(decl);
923   }
924
925   m_decls_to_deport = nullptr;
926   m_decls_already_deported = nullptr;
927 }
928
929 void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
930     clang::Decl *to, clang::Decl *from) {
931   ASTImporter::Imported(from, to);
932
933   /*
934   if (to_objc_interface)
935       to_objc_interface->startDefinition();
936
937   CXXRecordDecl *to_cxx_record = dyn_cast<CXXRecordDecl>(to);
938
939   if (to_cxx_record)
940       to_cxx_record->startDefinition();
941   */
942
943   Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
944
945   if (llvm::Error err = ImportDefinition(from)) {
946     LLDB_LOG_ERROR(log, std::move(err),
947                    "[ClangASTImporter] Error during importing definition: {0}");
948     return;
949   }
950
951   if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
952     if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
953       to_tag->setCompleteDefinition(from_tag->isCompleteDefinition());
954
955       if (Log *log_ast =
956               lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_AST)) {
957         std::string name_string;
958         if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
959           llvm::raw_string_ostream name_stream(name_string);
960           from_named_decl->printName(name_stream);
961           name_stream.flush();
962         }
963         LLDB_LOG(log_ast, "==== [ClangASTImporter][TUDecl: {0}] Imported "
964                           "({1}Decl*){2}, named {3} (from "
965                           "(Decl*){4})",
966                  static_cast<void *>(to->getTranslationUnitDecl()),
967                  from->getDeclKindName(), static_cast<void *>(to), name_string,
968                  static_cast<void *>(from));
969
970         // Log the AST of the TU.
971         std::string ast_string;
972         llvm::raw_string_ostream ast_stream(ast_string);
973         to->getTranslationUnitDecl()->dump(ast_stream);
974         LLDB_LOG(log_ast, "{0}", ast_string);
975       }
976     }
977   }
978
979   // If we're dealing with an Objective-C class, ensure that the inheritance
980   // has been set up correctly.  The ASTImporter may not do this correctly if
981   // the class was originally sourced from symbols.
982
983   if (ObjCInterfaceDecl *to_objc_interface = dyn_cast<ObjCInterfaceDecl>(to)) {
984     do {
985       ObjCInterfaceDecl *to_superclass = to_objc_interface->getSuperClass();
986
987       if (to_superclass)
988         break; // we're not going to override it if it's set
989
990       ObjCInterfaceDecl *from_objc_interface =
991           dyn_cast<ObjCInterfaceDecl>(from);
992
993       if (!from_objc_interface)
994         break;
995
996       ObjCInterfaceDecl *from_superclass = from_objc_interface->getSuperClass();
997
998       if (!from_superclass)
999         break;
1000
1001       llvm::Expected<Decl *> imported_from_superclass_decl =
1002           Import(from_superclass);
1003
1004       if (!imported_from_superclass_decl) {
1005         LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(),
1006                        "Couldn't import decl: {0}");
1007         break;
1008       }
1009
1010       ObjCInterfaceDecl *imported_from_superclass =
1011           dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
1012
1013       if (!imported_from_superclass)
1014         break;
1015
1016       if (!to_objc_interface->hasDefinition())
1017         to_objc_interface->startDefinition();
1018
1019       to_objc_interface->setSuperClass(m_source_ctx->getTrivialTypeSourceInfo(
1020           m_source_ctx->getObjCInterfaceType(imported_from_superclass)));
1021     } while (false);
1022   }
1023 }
1024
1025 void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
1026                                                      clang::Decl *to) {
1027   ClangASTMetrics::RegisterClangImport();
1028
1029   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1030
1031   // Some decls shouldn't be tracked here because they were not created by
1032   // copying 'from' to 'to'. Just exit early for those.
1033   if (m_decls_to_ignore.find(to) != m_decls_to_ignore.end())
1034     return clang::ASTImporter::Imported(from, to);
1035
1036   lldb::user_id_t user_id = LLDB_INVALID_UID;
1037   ClangASTMetadata *metadata = m_master.GetDeclMetadata(from);
1038   if (metadata)
1039     user_id = metadata->GetUserID();
1040
1041   if (log) {
1042     if (NamedDecl *from_named_decl = dyn_cast<clang::NamedDecl>(from)) {
1043       std::string name_string;
1044       llvm::raw_string_ostream name_stream(name_string);
1045       from_named_decl->printName(name_stream);
1046       name_stream.flush();
1047
1048       log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p, named %s (from "
1049                   "(Decl*)%p), metadata 0x%" PRIx64,
1050                   from->getDeclKindName(), static_cast<void *>(to),
1051                   name_string.c_str(), static_cast<void *>(from), user_id);
1052     } else {
1053       log->Printf("    [ClangASTImporter] Imported (%sDecl*)%p (from "
1054                   "(Decl*)%p), metadata 0x%" PRIx64,
1055                   from->getDeclKindName(), static_cast<void *>(to),
1056                   static_cast<void *>(from), user_id);
1057     }
1058   }
1059
1060   ASTContextMetadataSP to_context_md =
1061       m_master.GetContextMetadata(&to->getASTContext());
1062   ASTContextMetadataSP from_context_md =
1063       m_master.MaybeGetContextMetadata(m_source_ctx);
1064
1065   if (from_context_md) {
1066     OriginMap &origins = from_context_md->m_origins;
1067
1068     OriginMap::iterator origin_iter = origins.find(from);
1069
1070     if (origin_iter != origins.end()) {
1071       if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1072           user_id != LLDB_INVALID_UID) {
1073         if (origin_iter->second.ctx != &to->getASTContext())
1074           to_context_md->m_origins[to] = origin_iter->second;
1075       }
1076
1077       ImporterDelegateSP direct_completer =
1078           m_master.GetDelegate(&to->getASTContext(), origin_iter->second.ctx);
1079
1080       if (direct_completer.get() != this)
1081         direct_completer->ASTImporter::Imported(origin_iter->second.decl, to);
1082
1083       if (log)
1084         log->Printf("    [ClangASTImporter] Propagated origin "
1085                     "(Decl*)%p/(ASTContext*)%p from (ASTContext*)%p to "
1086                     "(ASTContext*)%p",
1087                     static_cast<void *>(origin_iter->second.decl),
1088                     static_cast<void *>(origin_iter->second.ctx),
1089                     static_cast<void *>(&from->getASTContext()),
1090                     static_cast<void *>(&to->getASTContext()));
1091     } else {
1092       if (m_decls_to_deport && m_decls_already_deported) {
1093         if (isa<TagDecl>(to) || isa<ObjCInterfaceDecl>(to)) {
1094           RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
1095           if (from_record_decl == nullptr ||
1096               !from_record_decl->isInjectedClassName()) {
1097             NamedDecl *to_named_decl = dyn_cast<NamedDecl>(to);
1098
1099             if (!m_decls_already_deported->count(to_named_decl))
1100               m_decls_to_deport->insert(to_named_decl);
1101           }
1102         }
1103       }
1104
1105       if (to_context_md->m_origins.find(to) == to_context_md->m_origins.end() ||
1106           user_id != LLDB_INVALID_UID) {
1107         to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1108       }
1109
1110       if (log)
1111         log->Printf("    [ClangASTImporter] Decl has no origin information in "
1112                     "(ASTContext*)%p",
1113                     static_cast<void *>(&from->getASTContext()));
1114     }
1115
1116     if (clang::NamespaceDecl *to_namespace =
1117             dyn_cast<clang::NamespaceDecl>(to)) {
1118       clang::NamespaceDecl *from_namespace =
1119           dyn_cast<clang::NamespaceDecl>(from);
1120
1121       NamespaceMetaMap &namespace_maps = from_context_md->m_namespace_maps;
1122
1123       NamespaceMetaMap::iterator namespace_map_iter =
1124           namespace_maps.find(from_namespace);
1125
1126       if (namespace_map_iter != namespace_maps.end())
1127         to_context_md->m_namespace_maps[to_namespace] =
1128             namespace_map_iter->second;
1129     }
1130   } else {
1131     to_context_md->m_origins[to] = DeclOrigin(m_source_ctx, from);
1132
1133     if (log)
1134       log->Printf("    [ClangASTImporter] Sourced origin "
1135                   "(Decl*)%p/(ASTContext*)%p into (ASTContext*)%p",
1136                   static_cast<void *>(from), static_cast<void *>(m_source_ctx),
1137                   static_cast<void *>(&to->getASTContext()));
1138   }
1139
1140   if (TagDecl *from_tag_decl = dyn_cast<TagDecl>(from)) {
1141     TagDecl *to_tag_decl = dyn_cast<TagDecl>(to);
1142
1143     to_tag_decl->setHasExternalLexicalStorage();
1144     to_tag_decl->getPrimaryContext()->setMustBuildLookupTable();
1145
1146     if (log)
1147       log->Printf(
1148           "    [ClangASTImporter] To is a TagDecl - attributes %s%s [%s->%s]",
1149           (to_tag_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1150           (to_tag_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1151           (from_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"),
1152           (to_tag_decl->isCompleteDefinition() ? "complete" : "incomplete"));
1153   }
1154
1155   if (isa<NamespaceDecl>(from)) {
1156     NamespaceDecl *to_namespace_decl = dyn_cast<NamespaceDecl>(to);
1157
1158     m_master.BuildNamespaceMap(to_namespace_decl);
1159
1160     to_namespace_decl->setHasExternalVisibleStorage();
1161   }
1162
1163   if (isa<ObjCContainerDecl>(from)) {
1164     ObjCContainerDecl *to_container_decl = dyn_cast<ObjCContainerDecl>(to);
1165
1166     to_container_decl->setHasExternalLexicalStorage();
1167     to_container_decl->setHasExternalVisibleStorage();
1168
1169     /*to_interface_decl->setExternallyCompleted();*/
1170
1171     if (log) {
1172       if (ObjCInterfaceDecl *to_interface_decl =
1173               llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1174         log->Printf(
1175             "    [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1176             "%s%s%s",
1177             (to_interface_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1178             (to_interface_decl->hasExternalVisibleStorage() ? " Visible" : ""),
1179             (to_interface_decl->hasDefinition() ? " HasDefinition" : ""));
1180       } else {
1181         log->Printf(
1182             "    [ClangASTImporter] To is an %sDecl - attributes %s%s",
1183             ((Decl *)to_container_decl)->getDeclKindName(),
1184             (to_container_decl->hasExternalLexicalStorage() ? " Lexical" : ""),
1185             (to_container_decl->hasExternalVisibleStorage() ? " Visible" : ""));
1186       }
1187     }
1188   }
1189 }
1190
1191 clang::Decl *
1192 ClangASTImporter::ASTImporterDelegate::GetOriginalDecl(clang::Decl *To) {
1193   ASTContextMetadataSP to_context_md =
1194       m_master.GetContextMetadata(&To->getASTContext());
1195
1196   if (!to_context_md)
1197     return nullptr;
1198
1199   OriginMap::iterator iter = to_context_md->m_origins.find(To);
1200
1201   if (iter == to_context_md->m_origins.end())
1202     return nullptr;
1203
1204   return const_cast<clang::Decl *>(iter->second.decl);
1205 }