[cfe-dev] Objective-C @defs support

Chris Lattner clattner at apple.com
Sat Jun 21 11:24:40 PDT 2008


On Jun 18, 2008, at 7:28 AM, David Chisnall wrote:
> This patch adds parsing and code generation support for @defs  
> directives (which allow you to create a C structure with the same  
> layout as an Objective-C object).

The patch looks great, some minor issues:

+++ lib/Parse/ParseDecl.cpp	(working copy)

Please update the grammar to indicate the changes you're making to the  
parser.



@@ -736,18 +736,33 @@
+    if(!Tok.is(tok::at)) {

Space after the 'if' please.

...

+    } else { // Handle @defs
+      ConsumeToken();
+      if (!Tok.isObjCAtKeyword(tok::objc_defs))
+        Diag(Tok, diag::err_unexpected_at);

Ok.

+      ConsumeToken();

Not ok.  This consumes the token after the @ even though you don't  
know what it is.  If it is <eof>, you're hosed :).  You need to think  
of a recovery strategy here, what should happen if an "@" is seen  
without defs after it?

+      if (!Tok.is(tok::l_paren)) Diag(Tok, diag::err_expected_lparen);
+      ConsumeParen();

Likewise, and below.  Also, please use ExpectAndConsume where possible.

+      llvm::SmallVector<DeclTy*, 16> Fields;
+      Actions.ActOnDefs(CurScope, Tok.getLocation(),  
Tok.getIdentifierInfo(),
+          Fields);
+      FieldDecls.insert(FieldDecls.end(), Fields.begin(),  
Fields.end());
+      ConsumeToken();
+      if (!Tok.is(tok::r_paren)) Diag(Tok, diag::err_expected_rparen);
+      ConsumeParen();
+    }


+++ lib/Sema/SemaDecl.cpp	(working copy)

+static void CollectIvars(ObjCInterfaceDecl *Class,
+    llvm::SmallVector<Sema::DeclTy*, 16> &ivars) {
+  if(Class->getSuperClass()) {
+    CollectIvars(Class->getSuperClass(), ivars);
+  }
+  for (ObjCInterfaceDecl::ivar_iterator iter=Class->ivar_begin() ;
+      iter!=Class->ivar_end() ; iter++) {
+    ivars.push_back(*iter);
+  }

Can this be: ivars.append(Class->ivar_begin(), Class->ivar_end())?

+}
+
+void Sema::ActOnDefs(Scope *S, SourceLocation DeclStart, IdentifierInfo
+      *ClassName, llvm::SmallVector<DeclTy*, 16> &Decls) {
+  ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
+  if (!Class) Diag(DeclStart, diag::err_undef_interface, ClassName- 
 >getName());
+  CollectIvars(Class, Decls);
+}
+

comments please!


+++ lib/CodeGen/CodeGenTypes.cpp	(working copy)
@@ -149,7 +149,7 @@

What is this patch doing?  It seems unrelated.








More information about the cfe-dev mailing list