[cfe-commits] r62197 - in /cfe/trunk: include/clang/AST/ASTContext.h include/clang/AST/Attr.h include/clang/Basic/DiagnosticKinds.def include/clang/Parse/AttributeList.h lib/AST/ASTContext.cpp lib/Parse/AttributeList.cpp lib/Sema/SemaDeclAttr.cpp lib/Sema/SemaExpr.cpp test/SemaObjC/nsobject-attribute.m

Fariborz Jahanian fjahanian at apple.com
Tue Jan 13 15:34:40 PST 2009


Author: fjahanian
Date: Tue Jan 13 17:34:40 2009
New Revision: 62197

URL: http://llvm.org/viewvc/llvm-project?rev=62197&view=rev
Log:
Implemenent objective-c's NSObject attribute as a way of ddeclaraing c-type
objects as an objective-c object.

Added:
    cfe/trunk/test/SemaObjC/nsobject-attribute.m
Modified:
    cfe/trunk/include/clang/AST/ASTContext.h
    cfe/trunk/include/clang/AST/Attr.h
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/include/clang/Parse/AttributeList.h
    cfe/trunk/lib/AST/ASTContext.cpp
    cfe/trunk/lib/Parse/AttributeList.cpp
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/AST/ASTContext.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/ASTContext.h?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/ASTContext.h (original)
+++ cfe/trunk/include/clang/AST/ASTContext.h Tue Jan 13 17:34:40 2009
@@ -356,6 +356,10 @@
   /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
   /// ID type).
   bool isObjCObjectPointerType(QualType Ty) const;
+  
+  /// isObjCNSObjectType - Return true if this is an NSObject object with
+  /// its NSObject attribute set.
+  bool isObjCNSObjectType(QualType Ty) const;
     
   //===--------------------------------------------------------------------===//
   //                         Type Sizing and Analysis

Modified: cfe/trunk/include/clang/AST/Attr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Attr.h?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Attr.h (original)
+++ cfe/trunk/include/clang/AST/Attr.h Tue Jan 13 17:34:40 2009
@@ -42,6 +42,7 @@
     NoReturn,
     NoThrow,
     ObjCGC,
+    ObjCNSObject,
     Packed,
     StdCall,
     TransparentUnion,
@@ -418,6 +419,15 @@
   static bool classof(const ObjCGCAttr *A) { return true; }
 };
 
+class ObjCNSObjectAttr : public Attr {
+// Implement isa/cast/dyncast/etc.
+public:
+  ObjCNSObjectAttr() : Attr(ObjCNSObject) {}
+  
+static bool classof(const Attr *A) { return A->getKind() == ObjCNSObject; }
+static bool classof(const ObjCNSObjectAttr *A) { return true; }
+};
+  
 class BlocksAttr : public Attr {
 public:
   enum BlocksAttrTypes {
@@ -433,7 +443,7 @@
   // Implement isa/cast/dyncast/etc.
 
   static bool classof(const Attr *A) { return A->getKind() == Blocks; }
-  static bool classof(const ObjCGCAttr *A) { return true; }
+  static bool classof(const BlocksAttr *A) { return true; }
 };
 
 }  // end namespace clang

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Tue Jan 13 17:34:40 2009
@@ -412,6 +412,8 @@
      "expected string literal or '[' for asm operand")
 DIAG(err_expected_selector_for_method, ERROR,
      "expected selector for Objective-C method")
+DIAG(err_nsobject_attribute, ERROR,
+     "__attribute ((NSObject)) is for pointer types only")
 
 DIAG(err_unexpected_at, ERROR,
      "unexpected '@' in program")

Modified: cfe/trunk/include/clang/Parse/AttributeList.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Parse/AttributeList.h?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/include/clang/Parse/AttributeList.h (original)
+++ cfe/trunk/include/clang/Parse/AttributeList.h Tue Jan 13 17:34:40 2009
@@ -76,6 +76,7 @@
     AT_blocks,
     AT_sentinel,
     AT_const,
+    AT_nsobject,
     UnknownAttribute
   };
   

Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Tue Jan 13 17:34:40 2009
@@ -2078,6 +2078,19 @@
 //                        Type Predicates.
 //===----------------------------------------------------------------------===//
 
+/// isObjCNSObjectType - Return true if this is an NSObject object using
+/// NSObject attribute on a c-style pointer type.
+/// FIXME - Make it work directly on types.
+///
+bool ASTContext::isObjCNSObjectType(QualType Ty) const {
+  if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
+    if (TypedefDecl *TD = TDT->getDecl())
+      if (TD->getAttr<ObjCNSObjectAttr>())
+        return true;
+  }
+  return false;  
+}
+
 /// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
 /// to an object type.  This includes "id" and "Class" (two 'special' pointers
 /// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
@@ -2101,7 +2114,11 @@
     return true;
   
   // If this a pointer to an interface (e.g. NSString*), it is ok.
-  return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
+  if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
+    return true;
+  
+  // If is has NSObject attribute, OK as well.
+  return isObjCNSObjectType(Ty);
 }
 
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/lib/Parse/AttributeList.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/AttributeList.cpp?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/AttributeList.cpp (original)
+++ cfe/trunk/lib/Parse/AttributeList.cpp Tue Jan 13 17:34:40 2009
@@ -81,6 +81,7 @@
     if (!memcmp(Str, "fastcall", 8)) return AT_fastcall;
     if (!memcmp(Str, "iboutlet", 8)) return AT_IBOutlet;
     if (!memcmp(Str, "sentinel", 8)) return AT_sentinel;
+    if (!memcmp(Str, "NSObject", 8)) return AT_nsobject;
     break;
   case 9:
     if (!memcmp(Str, "dllimport", 9)) return AT_dllimport;

Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Tue Jan 13 17:34:40 2009
@@ -578,6 +578,22 @@
   d->addAttr(new ObjCGCAttr(type));
 }
 
+static void HandleObjCNSObject(Decl *d, const AttributeList &Attr, Sema &S) {
+  if (Attr.getNumArgs() != 0) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
+    return;
+  }
+  if (TypedefDecl *TD = dyn_cast<TypedefDecl>(d)) {
+    QualType T = TD->getUnderlyingType();
+    if (!T->isPointerType() ||
+        !T->getAsPointerType()->getPointeeType()->isRecordType()) {
+      S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
+      return;
+    }
+  }
+  d->addAttr(new ObjCNSObjectAttr);
+}
+
 static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
   if (!Attr.getParameterName()) {    
     S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
@@ -1228,6 +1244,7 @@
     HandleTransparentUnionAttr(D, Attr, S);
     break;
   case AttributeList::AT_objc_gc:     HandleObjCGCAttr    (D, Attr, S); break;
+  case AttributeList::AT_nsobject:    HandleObjCNSObject  (D, Attr, S); break;
   case AttributeList::AT_blocks:      HandleBlocksAttr    (D, Attr, S); break;
   case AttributeList::AT_sentinel:    HandleSentinelAttr  (D, Attr, S); break;
   case AttributeList::AT_const:       HandleConstAttr     (D, Attr, S); break;

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=62197&r1=62196&r2=62197&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Jan 13 17:34:40 2009
@@ -3023,7 +3023,14 @@
   if (CompoundType.isNull()) {
     // Simple assignment "x = y".
     ConvTy = CheckSingleAssignmentConstraints(LHSType, RHS);
-    
+    // Special case of NSObject attributes on c-style pointer types.
+    if (ConvTy == IncompatiblePointer &&
+        ((Context.isObjCNSObjectType(LHSType) &&
+          Context.isObjCObjectPointerType(RHSType)) ||
+         (Context.isObjCNSObjectType(RHSType) &&
+          Context.isObjCObjectPointerType(LHSType))))
+      ConvTy = Compatible;
+  
     // If the RHS is a unary plus or minus, check to see if they = and + are
     // right next to each other.  If so, the user may have typo'd "x =+ 4"
     // instead of "x += 4".

Added: cfe/trunk/test/SemaObjC/nsobject-attribute.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaObjC/nsobject-attribute.m?rev=62197&view=auto

==============================================================================
--- cfe/trunk/test/SemaObjC/nsobject-attribute.m (added)
+++ cfe/trunk/test/SemaObjC/nsobject-attribute.m Tue Jan 13 17:34:40 2009
@@ -0,0 +1,36 @@
+// RUN: clang -fsyntax-only -verify %s
+
+typedef struct CGColor * __attribute__ ((NSObject)) CGColorRef;
+static int count;
+static CGColorRef tmp = 0;
+
+typedef struct S1  __attribute__ ((NSObject)) CGColorRef1; // expected-error {{__attribute ((NSObject)) is for pointer types only}}
+typedef void *  __attribute__ ((NSObject)) CGColorRef2; // expected-error {{__attribute ((NSObject)) is for pointer types only}}
+
+ at interface HandTested {
+ at public
+    CGColorRef x;
+}
+ at property(copy) CGColorRef x;
+ at end
+
+void setProperty(id self, id value)  {
+  ((HandTested *)self)->x = value;
+}
+
+id getProperty(id self) {
+     return (id)((HandTested *)self)->x;
+}
+
+ at implementation HandTested
+ at synthesize x=x;
+ at end
+
+int main(char *argc, char *argv[]) {
+    HandTested *to;
+    to.x = tmp;  // setter
+    if (tmp != to.x)
+      to.x = tmp;
+    return 0;
+}
+





More information about the cfe-commits mailing list