[cfe-commits] r52586 - in /cfe/trunk: include/clang/Basic/DiagnosticKinds.def include/clang/Parse/Action.h lib/Parse/ParseDecl.cpp lib/Sema/Sema.h lib/Sema/SemaDecl.cpp test/Sema/objc-at-defs.m

Chris Lattner sabre at nondot.org
Sat Jun 21 12:39:06 PDT 2008


Author: lattner
Date: Sat Jun 21 14:39:06 2008
New Revision: 52586

URL: http://llvm.org/viewvc/llvm-project?rev=52586&view=rev
Log:
add parser and sema support for the funny ObjC '@defs' thing.
Patch by David Chisnall!

Added:
    cfe/trunk/test/Sema/objc-at-defs.m
Modified:
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/include/clang/Parse/Action.h
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/lib/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaDecl.cpp

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

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Sat Jun 21 14:39:06 2008
@@ -331,6 +331,8 @@
      "expected identifier or '{'")
 DIAG(err_expected_lbrace, ERROR,
      "expected '{'")
+DIAG(err_expected_lparen, ERROR,
+     "expected '('")
 DIAG(err_expected_rparen, ERROR,
      "expected ')'")
 DIAG(err_expected_rsquare, ERROR,
@@ -922,6 +924,8 @@
      "ordered compare requires two args of floating point type ('%0' and '%1')")
 DIAG(err_typecheck_cond_expect_scalar, ERROR,
      "used type '%0' where arithmetic or pointer type is required")
+DIAG(err_typecheck_invalid_union_cast, ERROR,
+     "'%0' is not a member of '%1'")
 DIAG(ext_typecheck_cond_one_void, EXTENSION,
      "C99 forbids conditional expressions with only one void side")
 DIAG(ext_typecheck_cast_nonscalar, EXTENSION,

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

==============================================================================
--- cfe/trunk/include/clang/Parse/Action.h (original)
+++ cfe/trunk/include/clang/Parse/Action.h Sat Jun 21 14:39:06 2008
@@ -210,6 +210,11 @@
     return 0;
   }
   
+  /// Act on @defs() element found when parsing a structure.  ClassName is the
+  /// name of the referenced class.   
+  virtual void ActOnDefs(Scope *S, SourceLocation DeclStart,
+                         IdentifierInfo *ClassName,
+                         llvm::SmallVector<DeclTy*, 16> &Decls) {}
   virtual DeclTy *ActOnField(Scope *S, SourceLocation DeclStart,
                              Declarator &D, ExprTy *BitfieldWidth) {
     return 0;

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

==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Sat Jun 21 14:39:06 2008
@@ -707,7 +707,7 @@
 ///       struct-declaration-list:
 ///         struct-declaration
 ///         struct-declaration-list struct-declaration
-/// [OBC]   '@' 'defs' '(' class-name ')'                         [TODO]
+/// [OBC]   '@' 'defs' '(' class-name ')'
 ///
 void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
                                   unsigned TagType, DeclTy *TagDecl) {
@@ -736,18 +736,39 @@
     // Parse all the comma separated declarators.
     DeclSpec DS;
     FieldDeclarators.clear();
-    ParseStructDeclaration(DS, FieldDeclarators);
-    
-    // Convert them all to fields.
-    for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
-      FieldDeclarator &FD = FieldDeclarators[i];
-      // Install the declarator into the current TagDecl.
-      DeclTy *Field = Actions.ActOnField(CurScope,
-                                         DS.getSourceRange().getBegin(),
-                                         FD.D, FD.BitfieldSize);
-      FieldDecls.push_back(Field);
-    }
-    
+    if (!Tok.is(tok::at)) {
+      ParseStructDeclaration(DS, FieldDeclarators);
+      
+      // Convert them all to fields.
+      for (unsigned i = 0, e = FieldDeclarators.size(); i != e; ++i) {
+        FieldDeclarator &FD = FieldDeclarators[i];
+        // Install the declarator into the current TagDecl.
+        DeclTy *Field = Actions.ActOnField(CurScope,
+                                           DS.getSourceRange().getBegin(),
+                                           FD.D, FD.BitfieldSize);
+        FieldDecls.push_back(Field);
+      }
+    } else { // Handle @defs
+      ConsumeToken();
+      if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
+        Diag(Tok, diag::err_unexpected_at);
+        SkipUntil(tok::semi, true, true);
+        continue;
+      }
+      ConsumeToken();
+      ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
+      if (!Tok.is(tok::identifier)) {
+        Diag(Tok, diag::err_expected_ident);
+        SkipUntil(tok::semi, true, true);
+        continue;
+      }
+      llvm::SmallVector<DeclTy*, 16> Fields;
+      Actions.ActOnDefs(CurScope, Tok.getLocation(), Tok.getIdentifierInfo(),
+          Fields);
+      FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
+      ConsumeToken();
+      ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
+    } 
 
     if (Tok.is(tok::semi)) {
       ConsumeToken();

Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=52586&r1=52585&r2=52586&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Sat Jun 21 14:39:06 2008
@@ -240,6 +240,8 @@
   virtual DeclTy *ActOnTag(Scope *S, unsigned TagType, TagKind TK,
                            SourceLocation KWLoc, IdentifierInfo *Name,
                            SourceLocation NameLoc, AttributeList *Attr);
+  virtual void ActOnDefs(Scope *S, SourceLocation DeclStart, IdentifierInfo
+      *ClassName, llvm::SmallVector<DeclTy*, 16> &Decls);
   virtual DeclTy *ActOnField(Scope *S, SourceLocation DeclStart,
                              Declarator &D, ExprTy *BitfieldWidth);
   

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

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Jun 21 14:39:06 2008
@@ -1731,6 +1731,35 @@
   return New;
 }
 
+/// Collect the instance variables declared in an Objective-C object.  Used in
+/// the creation of structures from objects using the @defs directive.
+static void CollectIvars(ObjCInterfaceDecl *Class,
+                         llvm::SmallVector<Sema::DeclTy*, 16> &ivars) {
+  if (Class->getSuperClass())
+    CollectIvars(Class->getSuperClass(), ivars);
+  ivars.append(Class->ivar_begin(), Class->ivar_end());
+}
+
+/// Called whenever @defs(ClassName) is encountered in the source.  Inserts the
+/// instance variables of ClassName into Decls.
+void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart, 
+                     IdentifierInfo *ClassName,
+                     llvm::SmallVector<DeclTy*, 16> &Decls) {
+  // Check that ClassName is a valid class
+  ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
+  if (!Class) {
+    Diag(DeclStart, diag::err_undef_interface, ClassName->getName());
+    return;
+  }
+  // Add the isa pointer
+  Decls.push_back(FieldDecl::Create(Context, SourceLocation(),
+                                    &Context.Idents.get("isa"),
+                                    Context.getObjCClassType()));
+  // Collect the instance variables
+  CollectIvars(Class, Decls);
+}
+
+
 static bool CalcFakeICEVal(const Expr* Expr,
                            llvm::APSInt& Result,
                            ASTContext& Context) {

Added: cfe/trunk/test/Sema/objc-at-defs.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/objc-at-defs.m?rev=52586&view=auto

==============================================================================
--- cfe/trunk/test/Sema/objc-at-defs.m (added)
+++ cfe/trunk/test/Sema/objc-at-defs.m Sat Jun 21 14:39:06 2008
@@ -0,0 +1,29 @@
+// RUN: clang %s -fsyntax-only
+
+ at interface Test {
+	double a;
+}
+ at end
+ at implementation Test
+ at end
+ at interface TestObject : Test {
+ at public
+  float bar;
+  int foo;
+}
+ at end
+ at implementation TestObject
+ at end
+struct wibble {
+  @defs(TestObject)
+};
+
+
+int main(void)
+{
+	TestObject * a = (id)malloc(100);
+	a->foo = 12;
+	printf("12: %d\n", ((struct wibble*)a)->foo);
+	printf("%d: %d\n", ((char*)&(((struct wibble*)a)->foo)) - (char*)a, ((char*)&(a->foo)) - (char*)a);
+	return 0;
+}





More information about the cfe-commits mailing list