Source to source transformation : objc to c#

Hi,

Before spending some money I would like to know if what I want to do is possible or not.
I would need a tool to transform some objc code into c#, for instance let's consider the following dummy objc class :

@interface BankAccount: NSObject
{
    NSString *_name;
    NSString *_address;
}

+(id) bankAccountWithName:(NSString*)name address:(NSString*)addr;

-(id) initWithName:(NSString*)name address:(NSString*)addr;

-(void) dummyMethodJustToExplain;

@end

implemented like that

+(id) bankAccountWithName:(NSString*)name address:(NSString*)addr
{
    return [[[BankAccount alloc] initWithName:name address: addr] autorelease];
}

-(id) initWithName:(NSString*)name address:(NSString*)addr
{
   self = [super init];
   _name = name;
   _address = addr;
}

-(void) dummyMethodJustToExplain
{
    if (_name && _address != nil)
    {
      NSLog(@"Hello world");
    }
}

I would like to transform it in c# :

public class BankAccount : NSObject
{
    new public static BankAccount alloc() { return new BankAccount (); }

    protected NSString _name;
    protected NSString _address;

   public static id bankAccountWithName_address(NSString name, NSString addr)
   {
     return BankAccount.alloc().initWithName_address(name, addr);
   }

   public virtual id initWithName_address(NSString name, NSString addr)
   {
      id self = this;

      _name = name;
      _addr = addr;

      return self;
   }

   public virtual void dummyMethodJustToExplain()
   {
      if (_name != null && _address != null)
      {
        NS.Log(@"Hello world");
      }
   }
}

In the worst case I just need to transform the objc syntax into a C/# syntax ie instead of [myobj aMessage:10] I would like to get myObj.aMessage(10)
and replace nil by null and YES:NO by true/false.

Do you think llvm/clang/rewrite is appropriate to do that or am I asking too much ?

Yes, it is doable. We already have a translation tool to rewrite Objective-C into c++.
See Frontend/RewriteModernObjC.cpp as an example of how it i done.
This tool has a limitation and a drawback. It only works on pre-processed files.
Also, coding style in this tool is dated.

- Fariborz

and let's consider the following code :

/**<p>Returns whether the button cell is transparent.</p>
    <p>See Also: -setTransparent:</p>
  */
- (BOOL) isTransparent
{
   return _buttoncell_is_transparent;
}

/**<p>Sets whether the button cell is transparent.</p>
    <p>See Also: -isTransparent </p>
  */
- (void) setTransparent: (BOOL)flag
{
   _buttoncell_is_transparent = flag;
}

once transformed will it be possible to keep comments ?
Actually since clang is transforming the source code into an AST I am wondering if clang is not
too heavy for my task and will generate some ugly code.

Yes, it is doable. We already have a translation tool to rewrite
Objective-C into c++.
See Frontend/RewriteModernObjC.cpp as an example of how it i done.
This tool has a limitation and a drawback. It only works on
pre-processed files.
Also, coding style in this tool is dated.

and let's consider the following code :

/**<p>Returns whether the button cell is transparent.</p>
  <p>See Also: -setTransparent:</p>
*/
- (BOOL) isTransparent
{
return _buttoncell_is_transparent;
}

/**<p>Sets whether the button cell is transparent.</p>
  <p>See Also: -isTransparent </p>
*/
- (void) setTransparent: (BOOL)flag
{
_buttoncell_is_transparent = flag;
}

once transformed will it be possible to keep comments ?

Note that this specific translator works on the pre-processed file. If pre-processed files keep
the comment, this should not be a problem.

Actually since clang is transforming the source code into an AST I am wondering if clang is not
too heavy for my task and will generate some ugly code.

In the case of Objective-C translator, this tool makes minimal use of AST. It uses the AST in only
situations where a translation is needed (for ObjC methods). It produces a new AST and uses the rewriter to
replace the old piece of code with the new one. But, ObjC rewriter task is much easier than what you want because
it is writing into c++ code for which both AST and the rewriter library work. For C# this may not be as simple.
- fariborz

Yes. I have written a tool using libclang that translate C and Objective-C header files to D modules. This only translate the declarations and not the implementation.

It's written in D, but it's similar enough to C# to not be a problem to read it.

https://github.com/jacob-carlborg/dstep

libclang has functions for comment AST introspection so it should be possible to keep the comments, or even translate them to C# comments.

http://clang.llvm.org/doxygen/group__CINDEX__COMMENT.html