]> granicus.if.org Git - clang/commitdiff
More support for ivars in class extension.
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 23 Feb 2010 01:26:30 +0000 (01:26 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 23 Feb 2010 01:26:30 +0000 (01:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96850 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/DeclObjC.h
lib/AST/ASTContext.cpp
lib/AST/DeclObjC.cpp
lib/Sema/SemaDeclObjC.cpp
test/SemaObjC/ivar-in-class-extension.m [new file with mode: 0644]

index 686c1eb2f8b0cd2947b76fbd2b92fbf370f8ba92..26656bf30a65680b5b693c0dbfa840674be83b3a 100644 (file)
@@ -527,6 +527,8 @@ public:
     CategoryList = category;
   }
 
+  ObjCCategoryDecl* getClassExtension() const;
+  
   /// isSuperClassOf - Return true if this class is the specified class or is a
   /// super class of the specified interface class.
   bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
@@ -949,6 +951,20 @@ public:
 
   bool IsClassExtension() const { return getIdentifier() == 0; }
   
+  typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
+  ivar_iterator ivar_begin() const {
+    return ivar_iterator(decls_begin());
+  }
+  ivar_iterator ivar_end() const {
+    return ivar_iterator(decls_end());
+  }
+  unsigned ivar_size() const {
+    return std::distance(ivar_begin(), ivar_end());
+  }
+  bool ivar_empty() const {
+    return ivar_begin() == ivar_end();
+  }
+  
   SourceLocation getAtLoc() const { return AtLoc; }
   void setAtLoc(SourceLocation At) { AtLoc = At; }
 
index e8b84c65dd6a3cb6a2056c95c6785cc9f1ed6225..c8caeb62b31144054ef928e6f45b2ca0c26ff356 100644 (file)
@@ -900,6 +900,14 @@ void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
 ///
 void ASTContext::CollectNonClassIvars(const ObjCInterfaceDecl *OI,
                                 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
+  // Find ivars declared in class extension.
+  if (const ObjCCategoryDecl *CDecl = OI->getClassExtension()) {
+    for (ObjCCategoryDecl::ivar_iterator I = CDecl->ivar_begin(),
+         E = CDecl->ivar_end(); I != E; ++I) {
+      Ivars.push_back(*I);
+    }
+  }
+  
   for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
        E = OI->prop_end(); I != E; ++I) {
     if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
index 131e098d04678b282defa2c6c3a499b8dd294273..8decafa35e34a14dcd43fdf6a00eda3ef488ef23 100644 (file)
@@ -202,6 +202,17 @@ void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
   setProtocolList(ProtocolRefs.data(), NumProtoRefs, ProtocolLocs.data(), C);
 }
 
+/// getClassExtension - Find class extension of the given class.
+// FIXME. can speed it up, if need be.
+ObjCCategoryDecl* ObjCInterfaceDecl::getClassExtension() const {
+  const ObjCInterfaceDecl* ClassDecl = this;
+  for (ObjCCategoryDecl *CDecl = ClassDecl->getCategoryList(); CDecl;
+       CDecl = CDecl->getNextClassCategory())
+    if (CDecl->IsClassExtension())
+      return CDecl;
+  return 0;
+}
+
 ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
                                               ObjCInterfaceDecl *&clsDeclared) {
   ObjCInterfaceDecl* ClassDecl = this;
@@ -210,6 +221,12 @@ ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
       clsDeclared = ClassDecl;
       return I;
     }
+    if (const ObjCCategoryDecl *CDecl = ClassDecl->getClassExtension())
+      if (ObjCIvarDecl *I = CDecl->getIvarDecl(ID)) {
+        clsDeclared = ClassDecl;
+        return I;
+      }
+      
     ClassDecl = ClassDecl->getSuperClass();
   }
   return NULL;
index 5f69dc4462e3df1add5836df1c81d35a22868ae1..e40e8fedc07980201a4677f6ae87ba9cba48ecab 100644 (file)
@@ -599,13 +599,9 @@ ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
                             SourceLocation EndProtoLoc) {
   ObjCCategoryDecl *CDecl = 0;
   ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
-  if (!CategoryName) {
+  if (!CategoryName)
     // Class extensions require a special treatment. Use an existing one.
-    for (CDecl = IDecl->getCategoryList(); CDecl;
-         CDecl = CDecl->getNextClassCategory())
-      if (CDecl->IsClassExtension()) 
-        break;  
-  }
+    CDecl = IDecl->getClassExtension();
   if (!CDecl) {
     CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, ClassLoc,
                                      CategoryLoc, CategoryName);
diff --git a/test/SemaObjC/ivar-in-class-extension.m b/test/SemaObjC/ivar-in-class-extension.m
new file mode 100644 (file)
index 0000000..683a78f
--- /dev/null
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -fsyntax-only -fobjc-nonfragile-abi2 -verify %s
+
+@interface SomeClass  @end
+
+int fn1(SomeClass *obj) {
+        obj->privateIvar = 1; // expected-error {{'SomeClass' does not have a member named 'privateIvar}}
+        return obj->publicIvar;     // expected-error {{'SomeClass' does not have a member named 'publicIvar'}}
+}
+
+@interface SomeClass () {
+// @private by default
+        int privateIvar;
+@public
+        int publicIvar;
+}
+@end
+
+int fn2(SomeClass *obj) {
+       obj->publicIvar = 1;
+        return obj->publicIvar    // ok
+             + obj->privateIvar;  // expected-error {{instance variable 'privateIvar' is private}}
+}
+
+@implementation SomeClass
+
+int fn3(SomeClass *obj) {
+       obj->privateIvar = 2;
+        return obj->publicIvar    // ok
+             + obj->privateIvar;  // ok
+    }
+@end
+
+@interface SomeClass (Category)
+    {  // expected-error {{ivar may be placed in a class extension}}
+        int categoryIvar;
+    }
+@end
+
+@interface SomeClass (Category1)
+    {  // expected-error {{ivar may be placed in a class extension}}
+    }
+@end