    Ensure that DIType is regenerated after we visited an implementation that adds ivars to an interface. Fixes rdar://13175234

diff --git a/lib/AST/DeclObjC.cpp b/lib/AST/DeclObjC.cpp
index ce765a7..355e9dc 100644
--- a/lib/AST/DeclObjC.cpp
+++ b/lib/AST/DeclObjC.cpp
@@ -1077,41 +1077,50 @@ void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
 /// all_declared_ivar_begin - return first ivar declared in this class,
 /// its extensions and its implementation. Lazily build the list on first
 /// access.
+/// Caveat: If you invoke this method too early (i.e., before all
+/// implementations were parsed) the list of ivars will be
+/// incomplete. If you call it again after all implementations were
+/// visited, the list will also contain any ivars defined there.
 ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
   // FIXME: Should make sure no callers ever do this.
   if (!hasDefinition())
     return 0;
   
-  if (data().IvarList)
-    return data().IvarList;
-  
   ObjCIvarDecl *curIvar = 0;
-  if (!ivar_empty()) {
-    ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
-    data().IvarList = *I; ++I;
-    for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
-      curIvar->setNextIvar(*I);
-  }
+  if (!data().IvarList) {
+    if (!ivar_empty()) {
+      ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
+      data().IvarList = *I; ++I;
+      for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
+        curIvar->setNextIvar(*I);
+    }
 
-  for (ObjCInterfaceDecl::known_extensions_iterator
-         Ext = known_extensions_begin(),
-         ExtEnd = known_extensions_end();
-       Ext != ExtEnd; ++Ext) {
-    if (!Ext->ivar_empty()) {
-      ObjCCategoryDecl::ivar_iterator I = Ext->ivar_begin(),E = Ext->ivar_end();
-      if (!data().IvarList) {
-        data().IvarList = *I; ++I;
-        curIvar = data().IvarList;
+    for (ObjCInterfaceDecl::known_extensions_iterator
+           Ext = known_extensions_begin(),
+           ExtEnd = known_extensions_end();
+         Ext != ExtEnd; ++Ext) {
+      if (!Ext->ivar_empty()) {
+        ObjCCategoryDecl::ivar_iterator I = Ext->ivar_begin(),E = Ext->ivar_end();
+        if (!data().IvarList) {
+          data().IvarList = *I; ++I;
+          curIvar = data().IvarList;
+        }
+        for ( ;I != E; curIvar = *I, ++I)
+          curIvar->setNextIvar(*I);
       }
-      for ( ;I != E; curIvar = *I, ++I)
-        curIvar->setNextIvar(*I);
     }
+    data().ivarListMissingImplementation = true;
   }
+
+  // cached and complete!
+  if (!data().ivarListMissingImplementation)
+      return data().IvarList;
   
   if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
+    data().ivarListMissingImplementation = false;
     if (!ImplDecl->ivar_empty()) {
       ObjCImplementationDecl::ivar_iterator I = ImplDecl->ivar_begin(),
-                                            E = ImplDecl->ivar_end();
+        E = ImplDecl->ivar_end();
       if (!data().IvarList) {
         data().IvarList = *I; ++I;
         curIvar = data().IvarList;
diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index a4153d3..fa09586 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -1401,7 +1401,6 @@ llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
 				 RuntimeLang);
     return FwdDecl;
   }
-
   ID = Def;
 
   // Bit size, align and offset of the type.
@@ -1544,6 +1543,13 @@ llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
 
   llvm::DIArray Elements = DBuilder.getOrCreateArray(EltTys);
   FwdDeclNode->replaceOperandWith(10, Elements);
+
+  // If the implementation is not yet set, we do not want to mark it
+  // as complete. An implementation may declare additional
+  // private ivars that we would miss otherwise.
+  if (ID->getImplementation() == 0) {
+    CompletedTypeCache.erase(QualType(Ty, 0).getAsOpaquePtr());
+  }
   
   LexicalBlockStack.pop_back();
   return llvm::DIType(FwdDeclNode);
@@ -1810,7 +1816,17 @@ llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, llvm::DIFile Unit) {
   // And update the type cache.
   TypeCache[Ty.getAsOpaquePtr()] = Res;
 
-  if (!Res.isForwardDecl())
+  // clang::ParseAST handles each TopLevelDecl immediately after it was parsed.
+  // A subsequent implementation may add more ivars to an interface, which is
+  // why we cannot cache it yet.
+  bool possiblyIncomplete = false;
+  if (Ty->getTypeClass() == Type::ObjCInterface) {
+    ObjCInterfaceDecl* decl = cast<ObjCInterfaceType>(Ty)->getDecl();
+    if (decl)
+      possiblyIncomplete = (decl->getImplementation() == 0);
+  }
+
+  if (!Res.isForwardDecl() && !possiblyIncomplete)
     CompletedTypeCache[Ty.getAsOpaquePtr()] = Res;
 
   return Res;
diff --git a/test/CodeGenObjC/debug-info-ivars-private.m b/test/CodeGenObjC/debug-info-ivars-private.m
new file mode 100644
index 0000000..f1fd370
--- /dev/null
+++ b/test/CodeGenObjC/debug-info-ivars-private.m
@@ -0,0 +1,36 @@
+// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -emit-llvm -g %s -o - | FileCheck %s
+
+// rdar://13175234
+// Debug symbols for private IVars
+__attribute((objc_root_class)) @interface NSObject {
+	id isa;
+}
+@end
+
+@protocol Protocol
+@end
+
+@interface Delegate : NSObject<Protocol> {
+  @protected int foo;
+}
+@end
+
+@interface Delegate(NSObject)
+  - (void)f;
+@end
+
+@implementation Delegate(NSObject)
+- (void)f { return; }
+@end
+
+@implementation Delegate {
+  int bar;
+}
+
+- (void)g:(NSObject*) anObject {
+  bar = foo;
+}
+@end
+
+// CHECK: metadata !{i32 786445, metadata !{{[0-9]*}}, metadata !"foo", metadata !{{[0-9]*}}, i32 14, i64 32, i64 32, i64 0, i32 2, metadata !{{[0-9]*}}, null} ; [ DW_TAG_member ] [foo] [line 14, size 32, align 32, offset 0] [protected] [from int]
+// CHECK: metadata !{i32 786445, metadata !{{[0-9]*}}, metadata !"bar", metadata !{{[0-9]*}}, i32 27, i64 32, i64 32, i64 0, i32 1, metadata !{{[0-9]*}}, null} ; [ DW_TAG_member ] [bar] [line 27, size 32, align 32, offset 0] [private] [from int]
