6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
+ #include " Plugins/TypeSystem/Clang/ImporterBackedASTSource.h"
9
10
#include " lldb/Core/Module.h"
10
11
#include " lldb/Utility/LLDBAssert.h"
11
12
#include " lldb/Utility/LLDBLog.h"
17
18
#include " clang/AST/RecordLayout.h"
18
19
#include " clang/Sema/Lookup.h"
19
20
#include " clang/Sema/Sema.h"
21
+ #include " llvm/Support/Casting.h"
22
+ #include " llvm/Support/ErrorHandling.h"
20
23
#include " llvm/Support/raw_ostream.h"
21
24
22
25
#include " Plugins/ExpressionParser/Clang/ClangASTImporter.h"
@@ -230,35 +233,6 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener {
230
233
clang::ASTContext *m_src_ctx;
231
234
ClangASTImporter &importer;
232
235
233
- void CompleteDecl (
234
- Decl *decl,
235
- lldb_private::ClangASTImporter::ASTContextMetadata const &to_context_md) {
236
- // The decl that should be completed has to be imported into the target
237
- // context from some other context.
238
- assert (to_context_md.hasOrigin (decl));
239
- // We should only complete decls coming from the source context.
240
- assert (to_context_md.getOrigin (decl).ctx == m_src_ctx);
241
-
242
- Decl *original_decl = to_context_md.getOrigin (decl).decl ;
243
-
244
- // Complete the decl now.
245
- TypeSystemClang::GetCompleteDecl (m_src_ctx, original_decl);
246
- if (auto *tag_decl = dyn_cast<TagDecl>(decl)) {
247
- if (auto *original_tag_decl = dyn_cast<TagDecl>(original_decl)) {
248
- if (original_tag_decl->isCompleteDefinition ()) {
249
- m_delegate->ImportDefinitionTo (tag_decl, original_tag_decl);
250
- tag_decl->setCompleteDefinition (true );
251
- }
252
- }
253
-
254
- tag_decl->setHasExternalLexicalStorage (false );
255
- tag_decl->setHasExternalVisibleStorage (false );
256
- } else if (auto *container_decl = dyn_cast<ObjCContainerDecl>(decl)) {
257
- container_decl->setHasExternalLexicalStorage (false );
258
- container_decl->setHasExternalVisibleStorage (false );
259
- }
260
- }
261
-
262
236
public:
263
237
// / Constructs a CompleteTagDeclsScope.
264
238
// / \param importer The ClangASTImporter that we should observe.
@@ -280,9 +254,6 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener {
280
254
while (!m_decls_to_complete.empty ()) {
281
255
NamedDecl *decl = m_decls_to_complete.pop_back_val ();
282
256
m_decls_already_completed.insert (decl);
283
-
284
- CompleteDecl (decl, *to_context_md);
285
-
286
257
to_context_md->removeOrigin (decl);
287
258
}
288
259
@@ -296,6 +267,8 @@ class CompleteTagDeclsScope : public ClangASTImporter::NewDeclListener {
296
267
if (!isa<TagDecl>(to) && !isa<ObjCInterfaceDecl>(to))
297
268
return ;
298
269
270
+ to = ClangUtil::GetFirstDecl (to);
271
+
299
272
RecordDecl *from_record_decl = dyn_cast<RecordDecl>(from);
300
273
// We don't need to complete injected class name decls.
301
274
if (from_record_decl && from_record_decl->isInjectedClassName ())
@@ -510,14 +483,7 @@ bool ClangASTImporter::CompleteType(const CompilerType &compiler_type) {
510
483
if (!CanImport (compiler_type))
511
484
return false ;
512
485
513
- if (Import (compiler_type)) {
514
- TypeSystemClang::CompleteTagDeclarationDefinition (compiler_type);
515
- return true ;
516
- }
517
-
518
- TypeSystemClang::SetHasExternalStorage (compiler_type.GetOpaqueQualType (),
519
- false );
520
- return false ;
486
+ return Import (compiler_type);
521
487
}
522
488
523
489
// / Copy layout information from \ref source_map to the \ref destination_map.
@@ -666,8 +632,8 @@ bool ClangASTImporter::importRecordLayoutFromOrigin(
666
632
667
633
int field_idx = 0 , field_count = record_layout.getFieldCount ();
668
634
669
- for (RecordDecl::field_iterator fi = origin_record ->field_begin (),
670
- fe = origin_record ->field_end ();
635
+ for (RecordDecl::field_iterator fi = definition ->field_begin (),
636
+ fe = definition ->field_end ();
671
637
fi != fe; ++fi) {
672
638
if (field_idx >= field_count)
673
639
return false ; // Layout didn't go well. Bail out.
@@ -808,16 +774,34 @@ bool ClangASTImporter::CompleteTagDecl(clang::TagDecl *decl) {
808
774
if (!decl_origin.Valid ())
809
775
return false ;
810
776
811
- if (!TypeSystemClang::GetCompleteDecl (decl_origin.ctx , decl_origin.decl ))
777
+ auto *origin_def = llvm::cast<TagDecl>(decl_origin.decl )->getDefinition ();
778
+ if (!origin_def)
812
779
return false ;
813
780
814
781
ImporterDelegateSP delegate_sp (
815
782
GetDelegate (&decl->getASTContext (), decl_origin.ctx ));
816
783
817
784
ASTImporterDelegate::CxxModuleScope std_scope (*delegate_sp,
818
785
&decl->getASTContext ());
819
- if (delegate_sp)
820
- delegate_sp->ImportDefinitionTo (decl, decl_origin.decl );
786
+
787
+ // This is expected to pull in a definition for result_decl (if in redecl
788
+ // completion mode)
789
+ llvm::Expected<Decl *> result = delegate_sp->Import (origin_def);
790
+ if (!result) {
791
+ llvm::handleAllErrors (result.takeError (),
792
+ [](const clang::ASTImportError &e) {
793
+ llvm::errs () << " ERR: " << e.toString () << " \n " ;
794
+ });
795
+ return false ;
796
+ }
797
+
798
+ // Create redeclaration chain with the 'to' decls.
799
+ // Only need to do this if the 'result_decl' is a definition outside
800
+ // of any redeclaration chain and the input 'decl' was a forward declaration
801
+ TagDecl *result_decl = llvm::cast<TagDecl>(*result);
802
+ if (!decl->isThisDeclarationADefinition () && result_decl != decl)
803
+ if (result_decl->getPreviousDecl () == nullptr )
804
+ result_decl->setPreviousDecl (decl);
821
805
822
806
return true ;
823
807
}
@@ -838,89 +822,30 @@ bool ClangASTImporter::CompleteObjCInterfaceDecl(
838
822
if (!decl_origin.Valid ())
839
823
return false ;
840
824
841
- if (!TypeSystemClang::GetCompleteDecl (decl_origin.ctx , decl_origin.decl ))
825
+ ObjCInterfaceDecl *origin_decl =
826
+ llvm::cast<ObjCInterfaceDecl>(decl_origin.decl );
827
+
828
+ origin_decl = origin_decl->getDefinition ();
829
+ if (!origin_decl)
842
830
return false ;
843
831
844
832
ImporterDelegateSP delegate_sp (
845
833
GetDelegate (&interface_decl->getASTContext (), decl_origin.ctx ));
846
834
847
- if (delegate_sp)
848
- delegate_sp->ImportDefinitionTo (interface_decl, decl_origin.decl );
835
+ llvm::Expected<Decl *> result = delegate_sp->Import (origin_decl);
836
+ if (result)
837
+ return true ;
849
838
850
- if (ObjCInterfaceDecl *super_class = interface_decl->getSuperClass ())
851
- RequireCompleteType (clang::QualType (super_class->getTypeForDecl (), 0 ));
839
+ llvm::handleAllErrors (result.takeError (),
840
+ [](const clang::ASTImportError &e) {
841
+ llvm::errs () << " ERR: " << e.toString () << " \n " ;
842
+ });
852
843
853
- return true ;
844
+ return false ;
854
845
}
855
846
856
847
bool ClangASTImporter::CompleteAndFetchChildren (clang::QualType type) {
857
- if (!RequireCompleteType (type))
858
- return false ;
859
-
860
- Log *log = GetLog (LLDBLog::Expressions);
861
-
862
- if (const TagType *tag_type = type->getAs <TagType>()) {
863
- TagDecl *tag_decl = tag_type->getDecl ();
864
-
865
- DeclOrigin decl_origin = GetDeclOrigin (tag_decl);
866
-
867
- if (!decl_origin.Valid ())
868
- return false ;
869
-
870
- ImporterDelegateSP delegate_sp (
871
- GetDelegate (&tag_decl->getASTContext (), decl_origin.ctx ));
872
-
873
- ASTImporterDelegate::CxxModuleScope std_scope (*delegate_sp,
874
- &tag_decl->getASTContext ());
875
-
876
- TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl );
877
-
878
- for (Decl *origin_child_decl : origin_tag_decl->decls ()) {
879
- llvm::Expected<Decl *> imported_or_err =
880
- delegate_sp->Import (origin_child_decl);
881
- if (!imported_or_err) {
882
- LLDB_LOG_ERROR (log, imported_or_err.takeError (),
883
- " Couldn't import decl: {0}" );
884
- return false ;
885
- }
886
- }
887
-
888
- if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl))
889
- record_decl->setHasLoadedFieldsFromExternalStorage (true );
890
-
891
- return true ;
892
- }
893
-
894
- if (const ObjCObjectType *objc_object_type = type->getAs <ObjCObjectType>()) {
895
- if (ObjCInterfaceDecl *objc_interface_decl =
896
- objc_object_type->getInterface ()) {
897
- DeclOrigin decl_origin = GetDeclOrigin (objc_interface_decl);
898
-
899
- if (!decl_origin.Valid ())
900
- return false ;
901
-
902
- ImporterDelegateSP delegate_sp (
903
- GetDelegate (&objc_interface_decl->getASTContext (), decl_origin.ctx ));
904
-
905
- ObjCInterfaceDecl *origin_interface_decl =
906
- llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl );
907
-
908
- for (Decl *origin_child_decl : origin_interface_decl->decls ()) {
909
- llvm::Expected<Decl *> imported_or_err =
910
- delegate_sp->Import (origin_child_decl);
911
- if (!imported_or_err) {
912
- LLDB_LOG_ERROR (log, imported_or_err.takeError (),
913
- " Couldn't import decl: {0}" );
914
- return false ;
915
- }
916
- }
917
-
918
- return true ;
919
- }
920
- return false ;
921
- }
922
-
923
- return true ;
848
+ return RequireCompleteType (type);
924
849
}
925
850
926
851
bool ClangASTImporter::RequireCompleteType (clang::QualType type) {
@@ -1102,6 +1027,20 @@ ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
1102
1027
}
1103
1028
}
1104
1029
1030
+ if (auto *source = llvm::dyn_cast<ImporterBackedASTSource>(
1031
+ getToContext ().getExternalSource ())) {
1032
+ // We added a new declaration (which is not a definition) into the
1033
+ // destination AST context, so bump the declaration chain generation
1034
+ // counter.
1035
+ if (clang::TagDecl *td = dyn_cast<TagDecl>(From))
1036
+ if (!td->isThisDeclarationADefinition ())
1037
+ source->MarkRedeclChainsAsOutOfDate (getToContext ());
1038
+
1039
+ if (clang::ObjCInterfaceDecl *td = dyn_cast<ObjCInterfaceDecl>(From))
1040
+ if (!td->isThisDeclarationADefinition ())
1041
+ source->MarkRedeclChainsAsOutOfDate (getToContext ());
1042
+ }
1043
+
1105
1044
// If we have a forcefully completed type, try to find an actual definition
1106
1045
// for it in other modules.
1107
1046
const ClangASTMetadata *md = m_main.GetDeclMetadata (From);
@@ -1122,6 +1061,12 @@ ClangASTImporter::ASTImporterDelegate::ImportImpl(Decl *From) {
1122
1061
DeclContext::lookup_result lr = dc->lookup (*dn_or_err);
1123
1062
for (clang::Decl *candidate : lr) {
1124
1063
if (candidate->getKind () == From->getKind ()) {
1064
+ // If we're dealing with redecl chains, we want to find the definition,
1065
+ // so skip if the decl is actually just a forwad decl.
1066
+ if (auto *tag_decl = llvm::dyn_cast<TagDecl>(candidate);
1067
+ !tag_decl || !tag_decl->getDefinition ())
1068
+ continue ;
1069
+
1125
1070
RegisterImportedDecl (From, candidate);
1126
1071
m_decls_to_ignore.insert (candidate);
1127
1072
return candidate;
@@ -1223,27 +1168,6 @@ void ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(
1223
1168
}
1224
1169
}
1225
1170
1226
- // / Takes a CXXMethodDecl and completes the return type if necessary. This
1227
- // / is currently only necessary for virtual functions with covariant return
1228
- // / types where Clang's CodeGen expects that the underlying records are already
1229
- // / completed.
1230
- static void MaybeCompleteReturnType (ClangASTImporter &importer,
1231
- CXXMethodDecl *to_method) {
1232
- if (!to_method->isVirtual ())
1233
- return ;
1234
- QualType return_type = to_method->getReturnType ();
1235
- if (!return_type->isPointerType () && !return_type->isReferenceType ())
1236
- return ;
1237
-
1238
- clang::RecordDecl *rd = return_type->getPointeeType ()->getAsRecordDecl ();
1239
- if (!rd)
1240
- return ;
1241
- if (rd->getDefinition ())
1242
- return ;
1243
-
1244
- importer.CompleteTagDecl (rd);
1245
- }
1246
-
1247
1171
// / Recreate a module with its parents in \p to_source and return its id.
1248
1172
static OptionalClangModuleID
1249
1173
RemapModule (OptionalClangModuleID from_id,
@@ -1366,53 +1290,12 @@ void ClangASTImporter::ASTImporterDelegate::Imported(clang::Decl *from,
1366
1290
to_namespace_decl->setHasExternalVisibleStorage ();
1367
1291
}
1368
1292
1369
- MarkDeclImported (from, to);
1370
- }
1371
-
1372
- void ClangASTImporter::ASTImporterDelegate::MarkDeclImported (Decl *from,
1373
- Decl *to) {
1374
- Log *log = GetLog (LLDBLog::Expressions);
1375
-
1376
- if (auto *to_tag_decl = dyn_cast<TagDecl>(to)) {
1377
- to_tag_decl->setHasExternalLexicalStorage ();
1378
- to_tag_decl->getPrimaryContext ()->setMustBuildLookupTable ();
1379
- auto from_tag_decl = cast<TagDecl>(from);
1380
-
1381
- LLDB_LOG (
1382
- log,
1383
- " [ClangASTImporter] To is a TagDecl - attributes {0}{1} [{2}->{3}]" ,
1384
- (to_tag_decl->hasExternalLexicalStorage () ? " Lexical" : " " ),
1385
- (to_tag_decl->hasExternalVisibleStorage () ? " Visible" : " " ),
1386
- (from_tag_decl->isCompleteDefinition () ? " complete" : " incomplete" ),
1387
- (to_tag_decl->isCompleteDefinition () ? " complete" : " incomplete" ));
1293
+ if (clang::ObjCInterfaceDecl *td = dyn_cast<ObjCInterfaceDecl>(to)) {
1294
+ if (clang::ExternalASTSource *s = getToContext ().getExternalSource ())
1295
+ if (td->isThisDeclarationADefinition ())
1296
+ s->CompleteRedeclChain (td);
1297
+ td->setHasExternalVisibleStorage ();
1388
1298
}
1389
-
1390
- if (auto *to_container_decl = dyn_cast<ObjCContainerDecl>(to)) {
1391
- to_container_decl->setHasExternalLexicalStorage ();
1392
- to_container_decl->setHasExternalVisibleStorage ();
1393
-
1394
- if (log) {
1395
- if (ObjCInterfaceDecl *to_interface_decl =
1396
- llvm::dyn_cast<ObjCInterfaceDecl>(to_container_decl)) {
1397
- LLDB_LOG (
1398
- log,
1399
- " [ClangASTImporter] To is an ObjCInterfaceDecl - attributes "
1400
- " {0}{1}{2}" ,
1401
- (to_interface_decl->hasExternalLexicalStorage () ? " Lexical" : " " ),
1402
- (to_interface_decl->hasExternalVisibleStorage () ? " Visible" : " " ),
1403
- (to_interface_decl->hasDefinition () ? " HasDefinition" : " " ));
1404
- } else {
1405
- LLDB_LOG (
1406
- log, " [ClangASTImporter] To is an {0}Decl - attributes {1}{2}" ,
1407
- ((Decl *)to_container_decl)->getDeclKindName (),
1408
- (to_container_decl->hasExternalLexicalStorage () ? " Lexical" : " " ),
1409
- (to_container_decl->hasExternalVisibleStorage () ? " Visible" : " " ));
1410
- }
1411
- }
1412
- }
1413
-
1414
- if (clang::CXXMethodDecl *to_method = dyn_cast<CXXMethodDecl>(to))
1415
- MaybeCompleteReturnType (m_main, to_method);
1416
1299
}
1417
1300
1418
1301
clang::Decl *
0 commit comments