Rewriting question


to be able to rewrite objc code from [myObj someMsg:a withContext:ctx] into a java/c# syntax myObj.someMsg_withContext(a, ctx)
do I need to change the "nature" of the ast node and transforms it from an iVar into a c++ object that would have
a method named someMsg_withContext ?
IS there any sample code showing how to declare that kind of object ?
Inside RewriteModernObjC.cpp there is some code to synthetize a C function (SynthMsgSendFunctionDecl) but do you have some
sample code to generate a c++ method it would help I think.


Modern rewriter translates objective-c syntax for messaging into c function APIs which
implement the messaging. ASTs are those for a c function call. It does not introduce any
new AST. If you want to translate into other languages not
supported (in AST) by clang, your only choice is to rewriter the messaging syntax
directly into their c# representation.

  • Fariborz

Yes and how do I rewrite the messaging syntax directly ? What file or function is involved to do it ?

Rewriter works the next way: you visit AST nodes which are interesting for you,
take their SourceRanges's, and use Rewriter methods like ReplaceText or
RemoveText to replace _text_, corresponding to the node, with your _text_,
or remove it, respectively.

So, you change not AST nodes but replace strings inside source whith other strings.

Ok I understand now instead of focusing on trying to return a different Stmt I should only
use the text replace functions.
So next question when I call the rewriter on a file, let's say NSButton.m how can I "ask" the rewriter
to find amongst all the imports the one that contains the class declaration.
For instance with the following lines :

#include "config.h"

#import <Foundation/NSCharacterSet.h>

#import "AppKit/NSApplication.h"
#import "AppKit/NSButton.h"
#import "AppKit/NSButtonCell.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSWindow.h"

// class variables
static id buttonCellClass = nil;

    TODO Description
@implementation NSButton

// ...


it should find that the NSButton is declared inside AppKit/NSButton.h to be able to generate a single file with both
the declaration and definition. And while I am it how can I load the whole object hierarchy NSButton->NSControl->NSView->NSResponder->NSObject
to be able to know if an instance method "override" a method in a base class ?

As an example, you can look at RewriteObjCMethodDecl which builds a c function declaration and the caller,
then replaces the method declaration with this string; as in:

RewriteObjCMethodDecl(OMD->getClassInterface(), OMD, ResultStr);
    SourceLocation LocStart = OMD->getLocStart();
    SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart();

    const char *startBuf = SM->getCharacterData(LocStart);
    const char *endBuf = SM->getCharacterData(LocEnd);
    ReplaceText(LocStart, endBuf-startBuf, ResultStr);

- fariborz