ObjectiveC ARC. Introduce a new attribute, 'objc_bridge'
that teaches the compiler about a subset of toll-free 
bridging semantics. This is wip. // rdar://15454846


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194633 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index 09cc8be..46a85c5 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -207,6 +207,12 @@
   return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
 }
 
+static inline bool isCFRefType(TypedefNameDecl *TD, ASTContext &Ctx) {
+  StringRef TDName = TD->getIdentifier()->getName();
+  return ((TDName.startswith("CF") || TDName.startswith("CG")) &&
+          (TDName.rfind("Ref") != StringRef::npos));
+}
+
 static unsigned getNumAttributeArgs(const AttributeList &Attr) {
   // FIXME: Include the type in the argument list.
   return Attr.getNumArgs() + Attr.hasParsedType();
@@ -4389,6 +4395,50 @@
                            Attr.getAttributeSpellingListIndex()));
 }
 
+static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D,
+                                const AttributeList &Attr) {
+  if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
+    QualType T = TD->getUnderlyingType();
+    if (T->isPointerType()) {
+      T = T->getPointeeType();
+      if (T->isRecordType()) {
+        RecordDecl *RD = T->getAs<RecordType>()->getDecl();
+        if (!RD || RD->isUnion()) {
+          S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
+          << Attr.getRange() << Attr.getName() << ExpectedStruct;
+          return;
+        }
+      }
+    } else {
+      S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_pointertype);
+      return;
+    }
+    // Check for T being a CFType goes here.
+    if (!isCFRefType(TD, S.Context)) {
+      S.Diag(TD->getLocStart(), diag::err_objc_bridge_not_cftype);
+      return;
+    }
+  }
+  else {
+    S.Diag(D->getLocStart(), diag::err_objc_bridge_attribute);
+    return;
+  }
+  
+  if (Attr.getNumArgs() != 1) {
+    S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
+    return;
+  }
+  IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0;
+  if (!Parm) {
+    S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id);
+    return;
+  }
+  
+  D->addAttr(::new (S.Context)
+             ObjCBridgeAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0,
+                           Attr.getAttributeSpellingListIndex()));
+}
+
 static void handleObjCOwnershipAttr(Sema &S, Decl *D,
                                     const AttributeList &Attr) {
   if (hasDeclarator(D)) return;
@@ -4675,6 +4725,9 @@
       
   case AttributeList::AT_NSBridged:
     handleNSBridgedAttr(S, scope, D, Attr); break;
+      
+  case AttributeList::AT_ObjCBridge:
+    handleObjCBridgeAttr(S, scope, D, Attr); break;
 
   case AttributeList::AT_CFAuditedTransfer:
   case AttributeList::AT_CFUnknownTransfer: