Today I’d like to give you a HowTo for tapping the Mono C# compiler (mcs):
- Install Cygwin and don’t forget to configure the packages
makeandgcc-g++. - Download the Mono sources and extract the entire archive.
- Compile the parser generator
jayby typingmakein the directoryMONO-SRC\mcs\jay. - Go to the directory
MONO-SRC\mcs\mcsand run the parser generator
../jay/jay.exe -ctv < ../jay/skeleton.cs cs-parser.jay > cs-parser.cs. - Compile the Visual Studio solution “compiler.sln”.
After that Visual-Studio creates the obligatory mcs.exe in the current version. Now you are able to tap all the information during the compile process. In my case I’m only interested in the Abstract Syntax Tree (AST).
Unfortunately the Mono C# compiler retrives no fancy visitor like SUN’s Java compiler. All aspects are captured in an ugly and recursive class mixture. The entry point is the class RootContext.ToplevelTypes. So I recommend to add this class into the Visual Studio watch list to determine where you can find specific details.
The following snippet delivers the class, all methods and all arguments. Just put this code into driver.cs at line 1660, recompile it and look what the output shows.
foreach (Class c in RootContext.ToplevelTypes.Types) { Console.WriteLine("Class: " + c.Name); foreach ( Method m in c.Methods ) { Console.WriteLine("Method: " + m.Name); foreach (Parameter p in m.ParameterInfo.FixedParameters) { Console.WriteLine("Parameter: " + p.Name + "(" + p.TypeName + ")"); } } }







0 Responses to “Tapping the Mono C# compiler (mcs)”