Parsing objective-c message expressions

Hi,
I have just started looking at Clang and found it to be a great tool.
I also came across the RewriteObjc module and wanted to play around with how the Objective-C messages are "rewritten".

I have not been able to figure out how an assignment statement like:

c = [obj execute];

is parsed in Clang (what functions are called?). I would like to rewrite such statements differently from

[obj execute];

where nothing is being returned. Could someone please guide me which parts of code should I be looking at?

regards,
rajesh

Hi Rajesh,

In general, an excellent way to figure out the structure of the ASTs is to use the --ast-dump option:

   clang --ast-dump myfile.m

For example:

void f(NSObject *m) {
   NSObject* c = [m execute];
}

becomes:

void f(NSObject *m)
(CompoundStmt 0x2372060 <<stdin>:3:21, line:5:1>
   (DeclStmt 0x2371ff0 <line:4:3>
     0x2371fa0 "NSObject *c =
       (ImplicitCastExpr 0x236c030 <col:17, col:27> 'NSObject *'
         (ObjCMessageExpr 0x2372010 <col:17, col:27> 'id':'struct objc_object *' selector=execute
           (DeclRefExpr 0x2371fd0 <col:18> 'NSObject *' ParmVar='m' 0x23712b0)))")

Note that assignments and variable declarations are different. Assignments are represented using BinaryOperator, while variable declarations are represented using DeclStmt.

Ted

Hi,
I have just started looking at Clang and found it to be a great tool.
I also came across the RewriteObjc module and wanted to play around with
how the Objective-C messages are "rewritten".

I have not been able to figure out how an assignment statement like:

c = [obj execute];

is parsed in Clang (what functions are called?). I would like to rewrite
such statements differently from

[obj execute];

where nothing is being returned. Could someone please guide me which
parts of code should I be looking at?

Here is a brief example...

snaroffBook$ cat xx.m
#import <Foundation/NSObject.h>

void f(NSObject *m) {
   NSObject* c = [m execute];
}

snaroffBook$ ../../Debug/bin/clang -rewrite-objc xx.m

#include <Foundation/NSObject.h>

void f(NSObject *m) {
   NSObject* c = ((id (*)(id, SEL, ...))(void *)objc_msgSend)((id)m, sel_registerName("execute"));
}

The actual rewriter code is in RewriteObjC.cpp.

snaroff

Hi,
Is there a way to find the parent expression given an expression? Say I have an instance of ObjCMessageExpr and
want to find out if it’s part of a declaration (DeclStmt) or an statement by itself. Is there any facility to do that?

Thanks.

rajesh

Ted Kremenek wrote:

Hi Rajesh,

Please see my answer to this question in the following thread:

http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-August/002416.html

Ted