How to detect a reference is actually a field inside a macro ?

2 views
Skip to first unread message

emp...@gmail.com

unread,
Oct 12, 2009, 7:40:54 AM10/12/09
to Nemerle Forum
Hello ,

Assuming I have the following class

class Foo
{
public mutable x:int;

public Bar():void
{

x = 5;
}
}


I want to write a macro that parses the method body of Bar and it
should be able to detect "x" is actually a field of the class. How do
I do it ?

VladD2

unread,
Oct 12, 2009, 1:17:18 PM10/12/09
to nemer...@googlegroups.com
2009/10/12 emp...@gmail.com <emp...@gmail.com>:

> I want to write a macro that parses the method body of Bar and it
> should be able to detect "x" is actually a field of the class. How do
> I do it ?

You can type it code and and analyze it result:
...
typer.Manager.Solver.PushState();
try
{
match (pExpr)
{
| <[ $x = $_ ]> =>
def tExpr = typer.TypeExpr(x);
match (tExpr)
{
| TExpr...
}
}
}
finally { typer.Manager.Solver.PopState(); }

Or you can type all boby code (if it is correct) and then use
PExpr.TypeedObject property:
_ = typer.TypeExpr(body);
...
match (pExpr)
{
| <[ $x = $y ]> =>
match (x.TypeedObject)
{
| | TExpr...
}
}

--
С уважением,
Чистяков Владислав,
www.rsdn.ru

ReverseBlade

unread,
Oct 12, 2009, 11:36:46 PM10/12/09
to Nemerle Forum
Ok I understand but my problem is, My macro is an assembly level
macro and it loops all classes and all methods. So if I get a typer
with Macros.ImplicitCTX() it doesn't work because CurrentMethod of
TypeManager is null. This makes local_context in typer as null which
in turn produces null pointer exception.

So what I want is to have a typer for my method, while my macro
targets assembly or class. Is this possible ?

Ķaмĭļ ๏ Şκaļşκĭ

unread,
Oct 12, 2009, 11:44:39 PM10/12/09
to nemer...@googlegroups.com
2009/10/12 ReverseBlade <emp...@gmail.com>:
>
> Ok I understand but my problem is,  My macro is an assembly level
> macro and it loops all classes and all methods. So if I get a typer
> with Macros.ImplicitCTX() it doesn't work because CurrentMethod of
> TypeManager is null. This makes local_context in typer as null which
> in turn produces null pointer exception.
>
> So what I want is to have a typer for my method, while my macro
> targets assembly or class. Is this possible ?

You could inject macro invocation into each method. Otherwise you
would need to type each body of each method twice: once for you macro
and once for actual typing by compiler, which is quite wasteful.

Do something like:
method.Body = <[ my_method_level_macro ($(method.Body)) ]>


>
>
>
>  > I do it ?
>>
>> You can type it code and and analyze it result:
>> ...
>> typer.Manager.Solver.PushState();
>> try
>> {
>>   match (pExpr)
>>   {
>>     | <[ $x = $_ ]> =>
>>       def tExpr = typer.TypeExpr(x);
>>       match (tExpr)
>>       {
>>          | TExpr...
>>       }
>>   }}
>>
>> finally { typer.Manager.Solver.PopState(); }
>>
>> Or you can type all boby code (if it is correct) and then use
>> PExpr.TypeedObject property:
>> _ = typer.TypeExpr(body);
>> ...
>> match (pExpr)
>> {
>>   | <[ $x = $y ]> =>
>>     match (x.TypeedObject)
>>     {
>>       | | TExpr...
>>     }
>>
>> }
>>
>> --
>> С уважением,
>> Чистяков Владислав,www.rsdn.ru
> >
>



--
Kamil Skalski
http://kamil-skalski.pl

VladD2

unread,
Oct 12, 2009, 11:56:47 PM10/12/09
to nemer...@googlegroups.com
13 октября 2009 г. 7:36 пользователь ReverseBlade <emp...@gmail.com> написал:

>
> Ok I understand but my problem is,  My macro is an assembly level
> macro and it loops all classes and all methods. So if I get a typer
> with Macros.ImplicitCTX() it doesn't work because CurrentMethod of
> TypeManager is null. This makes local_context in typer as null which
> in turn produces null pointer exception.

What goal of you macro?

ReverseBlade

unread,
Oct 13, 2009, 1:46:33 AM10/13/09
to Nemerle Forum
The goal of my macro is to create a concept called "Pure" methods. I
define a "Pure" method as:

1-) It will not make any assignments to any Property or a field of any
class.
2-) Method body will contain invocations to Pure methods only (the
scope might be limited to current assembly since it is extremely hard
to cover other libraries like BCL. As an alternative, Pure methods
might just disallow calls to explicitly "UnPure" methods. In ths case
other methods might be assumed neutral.)
3-) All Property getters will be assumed Pure unless they are marked
"UnPure"

Basically a Pure method is something that doesn't change state of any
class even if it is allowed by a setter,a method or a mutable field.

For instance

class Foo
{
public int X {get;set; }

pure public void Bar()
{
X = 5 ; /// will be a Compilation error.The method is marked
pure so it cannot invoke setter.
}
}

///


class Foo2
{
public int X {get;set; }

pure public void Bar()
{
def y = X ; /// OK
}
}


On Oct 13, 6:56 am, VladD2 <v...@rsdn.ru> wrote:
> 13 октября 2009 г. 7:36 пользователь ReverseBlade <empe...@gmail.com> написал:

ReverseBlade

unread,
Oct 13, 2009, 1:53:30 AM10/13/09
to Nemerle Forum
The reason i want this to be a assembly level macro rather than method
level is, I want to be able to traverse all property getters in
current class where I assume them to be Pure without explicit marking
with "pure". So something like

[assembly:Purify] will traverse each property getter and check they
are "pure" or not.

VladD2

unread,
Oct 13, 2009, 9:37:43 AM10/13/09
to nemer...@googlegroups.com
13 октября 2009 г. 9:46 пользователь ReverseBlade <emp...@gmail.com> написал:

>
> The goal of my macro is to create a concept called "Pure" methods.  I
> define a "Pure" method as:

I think, it should implement in compiler.

P.S.

You read about D approach?
http://www.google.com/url?sa=t&source=web&ct=res&cd=1&ved=0CAcQFjAA&url=http%3A%2F%2Fwww.digitalmars.com%2Fd%2F2.0%2Faccu-functional.pdf&ei=XoLUSuL8KNCJ_gbTgpzZAg&usg=AFQjCNEQkfr0-pCW-dOc8dCUgurVPsFa6g&sig2=jU9eA_iVd61PjvYQg5wJTQ

ReverseBlade

unread,
Oct 13, 2009, 2:23:25 PM10/13/09
to Nemerle Forum
Hello,

I managed to get TypedObject. But there i can't see any information
about the variable points to a field or not.
What should I do ?

For D I saw the presentation. I want to try a simpler version with
macro.

On Oct 13, 4:37 pm, VladD2 <v...@rsdn.ru> wrote:
> 13 октября 2009 г. 9:46 пользователь ReverseBlade <empe...@gmail.com> написал:
>
>
>
> > The goal of my macro is to create a concept called "Pure" methods.  I
> > define a "Pure" method as:
>
> I think, it should implement in compiler.
>
> P.S.
>
> You read about D approach?http://www.google.com/url?sa=t&source=web&ct=res&cd=1&ved=0CAcQFjAA&u...

VladD2

unread,
Oct 13, 2009, 2:47:24 PM10/13/09
to nemer...@googlegroups.com
13 октября 2009 г. 22:23 пользователь ReverseBlade <emp...@gmail.com> написал:

> I managed to get TypedObject. But there i can't see any information
> about the variable points  to a field or not.
> What should I do ?

The bast place - a debugger! :)
Also, you can see DoType() method of Typer class
(https://nemerle.googlecode.com/svn/nemerle/trunk/ncc/typing/Typer.n)

If I am not wrong, field access can by TExpr.Member(x,
TExpr.FieldMember) or TExpr.FieldMember.

ReverseBlade

unread,
Oct 13, 2009, 3:24:37 PM10/13/09
to Nemerle Forum
I ran DoType() with debugger.

Unfortunately the TExpr is TExpr.LocalRef even if it points to a
field. I fear, for Typer's point of view, all method body variables
are assumed local. I think the information that local variable is
actually a field, becomes available in later stages of compiler :( .
Please correct me if I am wrong :(

On Oct 13, 9:47 pm, VladD2 <v...@rsdn.ru> wrote:
> 13 октября 2009 г. 22:23 пользователь ReverseBlade <empe...@gmail.com> написал:

VladD2

unread,
Oct 13, 2009, 4:10:18 PM10/13/09
to nemer...@googlegroups.com
13 октября 2009 г. 23:24 пользователь ReverseBlade <emp...@gmail.com> написал:

>
> I ran DoType() with debugger.
>
> Unfortunately the TExpr is  TExpr.LocalRef even if it points to a
> field. I fear, for Typer's point of view, all method body variables
> are assumed local. I think the information that local variable is
> actually a field, becomes available in later stages of compiler :(   .
> Please correct me if I am wrong :(

I think, you wrong. A TExpr.LocalRef refer into local variable.

Kamil, Im right?

Adam Skutt

unread,
Oct 13, 2009, 7:26:50 PM10/13/09
to nemer...@googlegroups.com
2009/10/13 ReverseBlade <emp...@gmail.com>:

>
> The reason i want this to be a assembly level macro rather than method
> level is, I want to be able to traverse all property getters in
> current class where I assume them to be Pure without explicit marking
> with "pure". So something like
>
That is a bad assumption: properties in .NET are just syntactic sugar
for methods and your code should explicitly treat them as such.

Plus, I don't really see the need for the assembly level macro:
perform the check the first time it's necessary and cache the result
between macro invocations.

Adam

ReverseBlade

unread,
Oct 13, 2009, 7:56:34 PM10/13/09
to Nemerle Forum
This is a bit irrelevant :)

On Oct 14, 2:26 am, Adam Skutt <ask...@gmail.com> wrote:
> 2009/10/13 ReverseBlade <empe...@gmail.com>:

ReverseBlade

unread,
Oct 13, 2009, 8:24:55 PM10/13/09
to Nemerle Forum
Sorry Vlad you are right and I was wrong.

It seems I Was missing something. TypedObject gives me the info what I
want. Sorry for bothering you and thanks for pointers.

VladD2

unread,
Oct 14, 2009, 2:28:47 PM10/14/09
to nemer...@googlegroups.com
2009/10/14 ReverseBlade <emp...@gmail.com>:

>
> Sorry Vlad you are right and I was wrong.
>
> It seems I Was missing something. TypedObject gives me the info what I
> want. Sorry for bothering you and thanks for pointers.

I think, this analysis should do in post typing process like Typer2 &
Typer3 classes (see it implementations) .

We can add entry point (event) which allow you add custom
"Typer-step". In this time, unfortunately, no PExpr-s exists, but
fortunately NOT exists Delayed-typer-actions and all macros will be
expended. It's very important!

ReverseBlade

unread,
Oct 14, 2009, 2:51:52 PM10/14/09
to Nemerle Forum
? A bit confused!

On Oct 14, 9:28 pm, VladD2 <v...@rsdn.ru> wrote:
> 2009/10/14 ReverseBlade <empe...@gmail.com>:

emp...@gmail.com

unread,
Oct 15, 2009, 4:32:08 AM10/15/09
to Nemerle Forum
Why this can't be done by an assembly level macro ?

Ķaмĭļ ๏ Şκaļşκĭ

unread,
Oct 15, 2009, 4:29:02 PM10/15/09
to nemer...@googlegroups.com
On Thu, Oct 15, 2009 at 1:32 AM, emp...@gmail.com <emp...@gmail.com> wrote:
>
> Why this can't be done by an assembly level macro ?

I guess it is a bit harder to predict which code constructs will be
transformed into exactly what calls / references in final code. But I
don't see real show stoppers, if there are any, this is a problem with
compler doing too much and restricting macros, which are in general
designed to do exactly this kinds of stuff.

See
http://nemerle.org/svn/nemerle/trunk/snippets/aop/src/macros/macros.n
this is general AOP framework, which does superset of what you are trying to do.
Reply all
Reply to author
Forward
0 new messages