[cfe-commits] r147417 - in /cfe/trunk: include/clang/Serialization/ASTReader.h lib/Serialization/ASTReaderDecl.cpp test/Modules/Inputs/redecl-merge-bottom.h test/Modules/Inputs/redecl-merge-left.h test/Modules/Inputs/redecl-merge-right.h test/Modules/redecl-merge.m

Douglas Gregor dgregor at apple.com
Sun Jan 1 13:47:52 PST 2012


Author: dgregor
Date: Sun Jan  1 15:47:52 2012
New Revision: 147417

URL: http://llvm.org/viewvc/llvm-project?rev=147417&view=rev
Log:
Implement declaration merging for Objective-C protocols across
multiple, disjoint modules. There is far too much duplicating with the
ObjCInterfaceDecl case here, which I'll eliminate shortly.

Modified:
    cfe/trunk/include/clang/Serialization/ASTReader.h
    cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
    cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h
    cfe/trunk/test/Modules/Inputs/redecl-merge-left.h
    cfe/trunk/test/Modules/Inputs/redecl-merge-right.h
    cfe/trunk/test/Modules/redecl-merge.m

Modified: cfe/trunk/include/clang/Serialization/ASTReader.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Serialization/ASTReader.h?rev=147417&r1=147416&r2=147417&view=diff
==============================================================================
--- cfe/trunk/include/clang/Serialization/ASTReader.h (original)
+++ cfe/trunk/include/clang/Serialization/ASTReader.h Sun Jan  1 15:47:52 2012
@@ -677,9 +677,9 @@
 
   /// \brief Reverse mapping from declarations to their global declaration IDs.
   /// 
-  /// FIXME: This data structure is currently only used for ObjCInterfaceDecls, 
-  /// support declaration merging. If we must have this for other declarations,
-  /// allocate it along with the Decl itself.
+  /// FIXME: This data structure is currently only used for ObjCInterfaceDecls
+  /// and ObjCProtocolDecls to support declaration merging. If we must have 
+  /// this for other declarations, allocate it along with the Decl itself.
   llvm::DenseMap<Decl *, serialization::GlobalDeclID> DeclToID;
   
   typedef llvm::DenseMap<Decl *, llvm::SmallVector<serialization::DeclID, 2> >

Modified: cfe/trunk/lib/Serialization/ASTReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTReaderDecl.cpp?rev=147417&r1=147416&r2=147417&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTReaderDecl.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTReaderDecl.cpp Sun Jan  1 15:47:52 2012
@@ -759,12 +759,54 @@
 }
 
 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
-  VisitRedeclarable(PD);
+  // Record the declaration -> global ID mapping.
+  Reader.DeclToID[PD] = ThisDeclID;
+  
+  RedeclarableResult Redecl = VisitRedeclarable(PD);
   VisitObjCContainerDecl(PD);
   PD->InitiallyForwardDecl = Record[Idx++];
   PD->isForwardProtoDecl = Record[Idx++];
   PD->setLocEnd(ReadSourceLocation(Record, Idx));
   
+  // Determine whether we need to merge this declaration with another @protocol
+  // with the same name.
+  // FIXME: Not needed unless the module file graph is a DAG.
+  if (FindExistingResult ExistingRes = findExisting(PD)) {
+    if (ObjCProtocolDecl *Existing = ExistingRes) {
+      ObjCProtocolDecl *ExistingCanon = Existing->getCanonicalDecl();
+      ObjCProtocolDecl *PDCanon = PD->getCanonicalDecl();
+      if (ExistingCanon != PDCanon) {
+        // Have our redeclaration link point back at the canonical declaration
+        // of the existing declaration, so that this declaration has the 
+        // appropriate canonical declaration.
+        PD->RedeclLink = ObjCProtocolDecl::PreviousDeclLink(ExistingCanon);
+        
+        // Don't introduce IDCanon into the set of pending declaration chains.
+        Redecl.suppress();
+        
+        // Introduce ExistingCanon into the set of pending declaration chains,
+        // if in fact it came from a module file.
+        if (ExistingCanon->isFromASTFile()) {
+          GlobalDeclID ExistingCanonID = Reader.DeclToID[ExistingCanon];
+          assert(ExistingCanonID && "Unrecorded canonical declaration ID?");
+          if (Reader.PendingDeclChainsKnown.insert(ExistingCanonID))
+            Reader.PendingDeclChains.push_back(ExistingCanonID);
+        }
+        
+        // If this declaration was the canonical declaration, make a note of 
+        // that. We accept the linear algorithm here because the number of 
+        // unique canonical declarations of an entity should always be tiny.
+        if (PDCanon == PD) {
+          SmallVectorImpl<DeclID> &Merged = Reader.MergedDecls[ExistingCanon];
+          if (std::find(Merged.begin(), Merged.end(), Redecl.getFirstID())
+                == Merged.end())
+            Merged.push_back(Redecl.getFirstID());
+        }
+      }
+    }
+  }
+
+  
   ObjCProtocolDecl *Def = ReadDeclAs<ObjCProtocolDecl>(Record, Idx);
   if (PD == Def) {
     // Read the definition.
@@ -1645,8 +1687,8 @@
          Y->getDeclContext()->getRedeclContext()))
     return false;
   
-  // Objective-C classes with the same name always match.
-  if (isa<ObjCInterfaceDecl>(X))
+  // Objective-C classes and protocols with the same name always match.
+  if (isa<ObjCInterfaceDecl>(X) || isa<ObjCProtocolDecl>(X))
     return true;
   
   // FIXME: Many other cases to implement.

Modified: cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h?rev=147417&r1=147416&r2=147417&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h (original)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-bottom.h Sun Jan  1 15:47:52 2012
@@ -2,6 +2,9 @@
 
 @class C4;
 @class C4;
+ at protocol P4;
+ at protocol P4;
+ at protocol P4;
 __import_module__ redecl_merge_right;
 
 @class B;

Modified: cfe/trunk/test/Modules/Inputs/redecl-merge-left.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/redecl-merge-left.h?rev=147417&r1=147416&r2=147417&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-left.h (original)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-left.h Sun Jan  1 15:47:52 2012
@@ -35,6 +35,10 @@
 
 struct explicit_struct;
 
+ at protocol P3, P4;
+
+ at protocol P3;
+
 #ifdef __cplusplus
 template<typename T> class Vector;
 

Modified: cfe/trunk/test/Modules/Inputs/redecl-merge-right.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/Inputs/redecl-merge-right.h?rev=147417&r1=147416&r2=147417&view=diff
==============================================================================
--- cfe/trunk/test/Modules/Inputs/redecl-merge-right.h (original)
+++ cfe/trunk/test/Modules/Inputs/redecl-merge-right.h Sun Jan  1 15:47:52 2012
@@ -42,6 +42,11 @@
 
 struct explicit_struct;
 
+ at protocol P4, P3;
+ at protocol P3;
+ at protocol P3;
+ at protocol P3;
+
 #ifdef __cplusplus
 template<typename T> class Vector { 
 public:

Modified: cfe/trunk/test/Modules/redecl-merge.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/redecl-merge.m?rev=147417&r1=147416&r2=147417&view=diff
==============================================================================
--- cfe/trunk/test/Modules/redecl-merge.m (original)
+++ cfe/trunk/test/Modules/redecl-merge.m Sun Jan  1 15:47:52 2012
@@ -6,6 +6,7 @@
 @class C3;
 __import_module__ redecl_merge_left;
 
+ at protocol P4;
 @class C3;
 @class C3;
 __import_module__ redecl_merge_right;
@@ -83,6 +84,13 @@
   [a init];
 }
 
+ at protocol P3
+- (void)p3_method;
+ at end
+
+id<P4> p4;
+id<P3> p3;
+
 #ifdef __cplusplus
 void testVector() {
   Vector<int> vec_int;





More information about the cfe-commits mailing list