This patch includes a conceptually simple, but very intrusive/pervasive change. 

The idea is to segregate Objective-C "object" pointers from general C pointers (utilizing the recently added ObjCObjectPointerType). The fun starts in Sema::GetTypeForDeclarator(), where "SomeInterface *" is now represented by a single AST node (rather than a PointerType whose Pointee is an ObjCInterfaceType). Since a significant amount of code assumed ObjC object pointers where based on C pointers/structs, this patch is very tedious. It should also explain why it is hard to accomplish this in smaller, self-contained patches.

This patch does most of the "heavy lifting" related to moving from PointerType->ObjCObjectPointerType. It doesn't include all potential "cleanups". The good news is additional cleanups can be done later (some are noted in the code). This patch is so large that I didn't want to include any changes that are purely aesthetic.

By making the ObjC types truly built-in, they are much easier to work with (and require fewer "hacks"). For example, there is no need for ASTContext::isObjCIdStructType() or ASTContext::isObjCClassStructType()! We believe this change (and the follow-up cleanups) will pay dividends over time. 

Given the amount of code change, I do expect some fallout from this change (though it does pass all of the clang tests). If you notice any problems, please let us know asap! Thanks.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@75314 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGDebugInfo.cpp b/lib/CodeGen/CGDebugInfo.cpp
index 8fa1e9c..e45fa55 100644
--- a/lib/CodeGen/CGDebugInfo.cpp
+++ b/lib/CodeGen/CGDebugInfo.cpp
@@ -761,8 +761,10 @@
     // Unsupported types
     return llvm::DIType();
   case Type::ObjCObjectPointer:   // Encode id<p> in debug info just like id.
-    return Slot = getOrCreateType(M->getContext().getObjCIdType(), Unit);
-      
+    {
+    ObjCObjectPointerType *OPT = cast<ObjCObjectPointerType>(Ty);
+    return Slot = CreateType(OPT->getInterfaceType(), Unit);
+    }
   case Type::ObjCQualifiedInterface:  // Drop protocols from interface.
   case Type::ObjCInterface: 
     return Slot = CreateType(cast<ObjCInterfaceType>(Ty), Unit);
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp
index 0951019..d42ca17 100644
--- a/lib/CodeGen/CGExpr.cpp
+++ b/lib/CodeGen/CGExpr.cpp
@@ -257,7 +257,6 @@
       Addr = Builder.CreateBitCast(Addr, MemTy, "storetmp");
     }
   }
-  
   Builder.CreateStore(Value, Addr, Volatile);  
 }
 
@@ -758,11 +757,11 @@
   default: assert(0 && "Unknown unary operator lvalue!");
   case UnaryOperator::Deref:
     {
-      QualType T =
-        E->getSubExpr()->getType()->getAsPointerType()->getPointeeType();
+      QualType T = E->getSubExpr()->getType()->getPointeeType();
+      assert(!T.isNull() && "CodeGenFunction::EmitUnaryOpLValue: Illegal type");
+        
       LValue LV = LValue::MakeAddr(EmitScalarExpr(E->getSubExpr()),
-                                   ExprTy->getAsPointerType()->getPointeeType()
-                                      .getCVRQualifiers(), 
+                                   T.getCVRQualifiers(), 
                                    getContext().getObjCGCAttrKind(T));
      // We should not generate __weak write barrier on indirect reference
      // of a pointer to object; as in void foo (__weak id *param); *param = 0;
@@ -900,7 +899,10 @@
     Address = Builder.CreateGEP(Base, Idx, "arrayidx");
   }
   
-  QualType T = E->getBase()->getType()->getAsPointerType()->getPointeeType();
+  QualType T = E->getBase()->getType()->getPointeeType();
+  assert(!T.isNull() && 
+         "CodeGenFunction::EmitArraySubscriptExpr(): Illegal base type");
+    
   LValue LV = LValue::MakeAddr(Address,
                                T.getCVRQualifiers(),
                                getContext().getObjCGCAttrKind(T));
@@ -1261,8 +1263,7 @@
   QualType ObjectTy;
   if (E->isArrow()) {
     BaseValue = EmitScalarExpr(BaseExpr);
-    const PointerType *PTy = BaseExpr->getType()->getAsPointerType();
-    ObjectTy = PTy->getPointeeType();
+    ObjectTy = BaseExpr->getType()->getPointeeType();
     CVRQualifiers = ObjectTy.getCVRQualifiers();
   } else {
     LValue BaseLV = EmitLValue(BaseExpr);
diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp
index 161cd57..94d0789 100644
--- a/lib/CodeGen/CGExprScalar.cpp
+++ b/lib/CodeGen/CGExprScalar.cpp
@@ -987,7 +987,7 @@
 }
 
 Value *ScalarExprEmitter::EmitAdd(const BinOpInfo &Ops) {
-  if (!Ops.Ty->isPointerType()) {
+  if (!Ops.Ty->isPointerType() && !Ops.Ty->isObjCObjectPointerType()) {
     if (CGF.getContext().getLangOptions().OverflowChecking &&
         Ops.Ty->isSignedIntegerType())
       return EmitOverflowCheckedBinOp(Ops);
@@ -998,20 +998,24 @@
     return Builder.CreateAdd(Ops.LHS, Ops.RHS, "add");
   }
 
-  if (Ops.Ty->getAsPointerType()->isVariableArrayType()) {
+  if (Ops.Ty->isPointerType() &&
+      Ops.Ty->getAsPointerType()->isVariableArrayType()) {
     // The amount of the addition needs to account for the VLA size
     CGF.ErrorUnsupported(Ops.E, "VLA pointer addition");
   }
   Value *Ptr, *Idx;
   Expr *IdxExp;
-  const PointerType *PT;
-  if ((PT = Ops.E->getLHS()->getType()->getAsPointerType())) {
+  const PointerType *PT = Ops.E->getLHS()->getType()->getAsPointerType();
+  const ObjCObjectPointerType *OPT = 
+    Ops.E->getLHS()->getType()->getAsObjCObjectPointerType();
+  if (PT || OPT) {
     Ptr = Ops.LHS;
     Idx = Ops.RHS;
     IdxExp = Ops.E->getRHS();
-  } else {                                           // int + pointer
+  } else {  // int + pointer
     PT = Ops.E->getRHS()->getType()->getAsPointerType();
-    assert(PT && "Invalid add expr");
+    OPT = Ops.E->getRHS()->getType()->getAsObjCObjectPointerType();
+    assert((PT || OPT) && "Invalid add expr");
     Ptr = Ops.RHS;
     Idx = Ops.LHS;
     IdxExp = Ops.E->getLHS();
@@ -1027,8 +1031,7 @@
     else
       Idx = Builder.CreateZExt(Idx, IdxType, "idx.ext");
   }
-
-  const QualType ElementType = PT->getPointeeType();
+  const QualType ElementType = PT ? PT->getPointeeType() : OPT->getPointeeType();
   // Handle interface types, which are not represented with a concrete
   // type.
   if (const ObjCInterfaceType *OIT = dyn_cast<ObjCInterfaceType>(ElementType)) {
@@ -1066,7 +1069,8 @@
     return Builder.CreateSub(Ops.LHS, Ops.RHS, "sub");
   }
 
-  if (Ops.E->getLHS()->getType()->getAsPointerType()->isVariableArrayType()) {
+  if (Ops.E->getLHS()->getType()->isPointerType() &&
+      Ops.E->getLHS()->getType()->getAsPointerType()->isVariableArrayType()) {
     // The amount of the addition needs to account for the VLA size for
     // ptr-int
     // The amount of the division needs to account for the VLA size for
@@ -1075,7 +1079,7 @@
   }
 
   const QualType LHSType = Ops.E->getLHS()->getType();
-  const QualType LHSElementType = LHSType->getAsPointerType()->getPointeeType();
+  const QualType LHSElementType = LHSType->getPointeeType();
   if (!isa<llvm::PointerType>(Ops.RHS->getType())) {
     // pointer - int
     Value *Idx = Ops.RHS;
diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp
index 33cb5bc..5d37841 100644
--- a/lib/CodeGen/CGObjC.cpp
+++ b/lib/CodeGen/CGObjC.cpp
@@ -305,8 +305,8 @@
 QualType CodeGenFunction::TypeOfSelfObject() {
   const ObjCMethodDecl *OMD = cast<ObjCMethodDecl>(CurFuncDecl);
   ImplicitParamDecl *selfDecl = OMD->getSelfDecl();
-  const PointerType *PTy = 
-    cast<PointerType>(getContext().getCanonicalType(selfDecl->getType()));
+  const ObjCObjectPointerType *PTy = cast<ObjCObjectPointerType>(
+    getContext().getCanonicalType(selfDecl->getType()));
   return PTy->getPointeeType();
 }
 
diff --git a/lib/CodeGen/CGObjCGNU.cpp b/lib/CodeGen/CGObjCGNU.cpp
index 46e948e..a75b39a 100644
--- a/lib/CodeGen/CGObjCGNU.cpp
+++ b/lib/CodeGen/CGObjCGNU.cpp
@@ -1338,7 +1338,7 @@
         Handlers.push_back(std::make_pair(CatchDecl, CatchStmt->getCatchBody()));
 
         // @catch() and @catch(id) both catch any ObjC exception
-        if (!CatchDecl || CGF.getContext().isObjCIdType(CatchDecl->getType())
+        if (!CatchDecl || CatchDecl->getType()->isObjCIdType()
             || CatchDecl->getType()->isObjCQualifiedIdType()) {
           // Use i8* null here to signal this is a catch all, not a cleanup.
           ESelArgs.push_back(NULLPtr);
@@ -1348,10 +1348,11 @@
         } 
 
         // All other types should be Objective-C interface pointer types.
-        const PointerType *PT = CatchDecl->getType()->getAsPointerType();
-        assert(PT && "Invalid @catch type.");
+        const ObjCObjectPointerType *OPT = 
+          CatchDecl->getType()->getAsObjCObjectPointerType();
+        assert(OPT && "Invalid @catch type.");
         const ObjCInterfaceType *IT = 
-          PT->getPointeeType()->getAsObjCInterfaceType();
+          OPT->getPointeeType()->getAsObjCInterfaceType();
         assert(IT && "Invalid @catch type.");
         llvm::Value *EHType =
           MakeConstantString(IT->getDecl()->getNameAsString());
diff --git a/lib/CodeGen/CGObjCMac.cpp b/lib/CodeGen/CGObjCMac.cpp
index fadbdd7..82ab88f 100644
--- a/lib/CodeGen/CGObjCMac.cpp
+++ b/lib/CodeGen/CGObjCMac.cpp
@@ -2523,19 +2523,18 @@
       llvm::BasicBlock *NextCatchBlock = CGF.createBasicBlock("catch");
 
       const ParmVarDecl *CatchParam = CatchStmt->getCatchParamDecl();
-      const PointerType *PT = 0;
+      const ObjCObjectPointerType *OPT = 0;
 
       // catch(...) always matches.
       if (!CatchParam) {
         AllMatched = true;
       } else {
-        PT = CatchParam->getType()->getAsPointerType();
+        OPT = CatchParam->getType()->getAsObjCObjectPointerType();
         
         // catch(id e) always matches. 
         // FIXME: For the time being we also match id<X>; this should
         // be rejected by Sema instead.
-        if ((PT && CGF.getContext().isObjCIdStructType(PT->getPointeeType())) ||
-            CatchParam->getType()->isObjCQualifiedIdType())
+        if (OPT && (OPT->isObjCIdType()) || OPT->isObjCQualifiedIdType())
           AllMatched = true;
       }
       
@@ -2551,8 +2550,8 @@
         break;
       }
       
-      assert(PT && "Unexpected non-pointer type in @catch");
-      QualType T = PT->getPointeeType();
+      assert(OPT && "Unexpected non-object pointer type in @catch");
+      QualType T = OPT->getPointeeType();
       const ObjCInterfaceType *ObjCType = T->getAsObjCInterfaceType();
       assert(ObjCType && "Catch parameter must have Objective-C type!");
 
@@ -5443,7 +5442,7 @@
           break;
         }
 
-        if (CGF.getContext().isObjCIdType(CatchDecl->getType()) ||
+        if (CatchDecl->getType()->isObjCIdType() ||
             CatchDecl->getType()->isObjCQualifiedIdType()) {
           llvm::Value *IDEHType = 
             CGM.getModule().getGlobalVariable("OBJC_EHTYPE_id");
@@ -5459,10 +5458,10 @@
         } 
 
         // All other types should be Objective-C interface pointer types.
-        const PointerType *PT = CatchDecl->getType()->getAsPointerType();
+        const ObjCObjectPointerType *PT = 
+          CatchDecl->getType()->getAsObjCObjectPointerType();
         assert(PT && "Invalid @catch type.");
-        const ObjCInterfaceType *IT = 
-          PT->getPointeeType()->getAsObjCInterfaceType();
+        const ObjCInterfaceType *IT = PT->getInterfaceType();
         assert(IT && "Invalid @catch type.");
         llvm::Value *EHType = GetInterfaceEHType(IT->getDecl(), false);
         SelectorArgs.push_back(EHType);
diff --git a/lib/CodeGen/CodeGenTypes.cpp b/lib/CodeGen/CodeGenTypes.cpp
index 1a30ea3..30a69f1 100644
--- a/lib/CodeGen/CodeGenTypes.cpp
+++ b/lib/CodeGen/CodeGenTypes.cpp
@@ -215,6 +215,7 @@
 const llvm::Type *CodeGenTypes::ConvertNewType(QualType T) {
   const clang::Type &Ty = *Context.getCanonicalType(T);
   
+  //T->dump();
   switch (Ty.getTypeClass()) {
 #define TYPE(Class, Base)
 #define ABSTRACT_TYPE(Class, Base)
@@ -353,10 +354,14 @@
     return T;
   }
       
-  case Type::ObjCObjectPointer:
-    // Protocols don't influence the LLVM type.
-    return ConvertTypeRecursive(Context.getObjCIdType());
-
+  case Type::ObjCObjectPointer: {
+   // Qualified id types don't influence the LLVM type, here we always return
+   // an opaque type for 'id'.
+   const llvm::Type *&T = InterfaceTypes[0];
+   if (!T)
+       T = llvm::OpaqueType::get();
+   return llvm::PointerType::getUnqual(T);
+  }
   case Type::Record:
   case Type::Enum: {
     const TagDecl *TD = cast<TagType>(Ty).getDecl();
diff --git a/lib/CodeGen/Mangle.cpp b/lib/CodeGen/Mangle.cpp
index 8018b4f..97c26f8 100644
--- a/lib/CodeGen/Mangle.cpp
+++ b/lib/CodeGen/Mangle.cpp
@@ -480,6 +480,11 @@
     Out << 'P';
     mangleType(PT->getPointeeType());
   }
+  else if (const ObjCObjectPointerType *PT = 
+           dyn_cast<ObjCObjectPointerType>(T.getTypePtr())) {
+    Out << 'P';
+    mangleType(PT->getPointeeType());
+  }
   //         ::= R <type>   # reference-to
   else if (const LValueReferenceType *RT =
            dyn_cast<LValueReferenceType>(T.getTypePtr())) {