[Mono-devel-list] Extension to mcs

Kamil Skalski nazgul at omega.pl
Thu Apr 22 16:29:23 EDT 2004

Thursday 22 of April 2004 21:26, Jürg Billeter wrote:
> Hi
> I'd like to add support for "Design by Contract" [1] to the Mono C#
> compiler using custom attributes - kind of what eXtensible C# [2] does.

It's quite interesting, as we work on similar thing in Nemerle 
(http://nemerle.org). Our language introduces general compile-time macros. We 
already have working meta-programming system transforming parse-trees just 
before typing procedures during compilation. We have also special system of 
"code quotation" (derived from Template Haskell, Scheme, etc.) to build and 
decompose parsetrees. 

Our macros are currently used like common functions
def k = 5;
print ("There are $k nodes");  // print is a macro which prints value of k

(they transform syntax-trees passed to them by parameters and returns new 
syntax tree, which is inlined in the place where they occur)

Next stage of our development with regard to macros is placing their 
invocation in custom attributes.
class Customer {
public Foo ([NotNull] o : object) : void { .. }

so we can algorithmically transform classes and their bodies at compile-time.

So, any design decision you will make about macros in attributes would be very 
interesting for me, as I will also implement them in near future.

> This requires the dynamic modification of the code tree after the parse
> step of mcs. At the moment there is AFAIK no possibility to extend mcs

In general it is possible to embedd macro system into compiler in a very 
elegant way. In Nemerle we just have one or two places between parsing and 
typing, where macro expansion is done. Much more work comes when you want to 
make manipulation of given syntax trees easier for programmer - this involves 
quotation system, hygine, lexical scoping, etc. It is also very interesting 
for me what design would you choose to manipulate code. As I looked at 
eXtensible C# they use pure (and hard to read) API to do this.

> like that without forking it. But it seems that such extensions are
> possible with very little modification of mcs. Specifically that'd be:
> - a hook event after the parse step, probably in MainDriver
> - a public get property for the statements member in the Block class

I'm also curious if macro system could be as relatively easily added to mcs 
compiler as in Nemerle. 

> Perhaps more changes will become necessary but such little modifications
> shouldn't hurt. So, my question is now, can I assume that a patch
> integrating these changes would be accepted or are there some
> fundamental issues I've overlooked?

Compiling and loading of code transformation programs comes into my mind, but 
it can be also solved easily. Much depends on power which you would like to 
give your custom attributes and design decisions about how they are written.

Kamil Skalski
Nemerle developer;

More information about the Mono-devel-list mailing list