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