Microsoft released a public preview of the .NET Compiler Platform, codenamed Roslyn, on April 3rd, 2014. The code is available at http://roslyn.codeplex.com/ for you to bask in all of it’s glory.
You can clone the .NET Compiler Platform Git repository using this command:
git clone https://git01.codeplex.com/roslyn
Or install the Nuget Package:
Install-Package Microsoft.CodeAnalysis -Pre
What is the .NET Compiler Platform?
Traditionally, compilers are black boxes — source code goes in one end, magic happens in the middle, and object files or assemblies come out the other end. As compilers perform their magic, they build up deep understanding of the code they are processing, but that knowledge is unavailable to anyone but the compiler implementation wizards. The information is promptly forgotten after the translated output is produced.
This is the core mission of the .NET Compiler Platform (“Roslyn”): opening up the black boxes and allowing tools and end users to share in the wealth of information compilers have about our code. Instead of being opaque source-code-in and object-code-out translators, through the .NET Compiler Platform (“Roslyn”), compilers become platforms—APIs that you can use for code related tasks in your tools and applications.
Microsoft took the original C# and Visual Basic compilers, which were written mostly in C++, and completely rewrote them in managed code. This means they were able to create a set of APIs that allow you to consume the code compilation and analysis results. There are currently 2 main APIs: The [Compiler APIs](http://roslyn.codeplex.com/wikipage?title=Overview&referringTitle=Home#Compiler APIs "Roslyn Compiler APIs") and [Workspace APIs](http://roslyn.codeplex.com/wikipage?title=Overview&referringTitle=Home#Workspaces APIs "Roslyn Workspace APIs"). It is worth noting that neither of these APIs have a dependency on Visual Studio which means you can provide much of the same Visual Studio functionality in any application you want.
The Compiler API layer allows you to view information about the compilation process. This includes syntax and semantic information, errors, warnings, as well as access to files and information after compilation is complete. It provides Syntax Trees that display the structure and references between your code, Syntax Tokens which are the keywords, variables, etc in your code, and Syntax Trivia which is essentially the items that the compiler ignores such as whitespace and comments.
The Workspace APIs provide you information about the current project and solution, allowing quick and easy access to a vast array of information about the code. This assists in providing code analysis, refactoring, and Intellisense to the user. The Workspace API has a
CurrentSolution property that gets updated whenever a change to the host environment occurs. This can be anything from typing a letter in a source file to saving a project.
Why is this cool?
Well, first off, it’s open source! The .NET Compiler Platform is part of Microsoft’s newly created .NET Foundation, which is a foundation created to help spur on development of open source technologies making use of .NET. Open source means that the community at large can review the code, provide bug reports and fixes, and can maintain the code even if Microsoft falls off the face of the earth. The fact that Microsoft open sourced these compilers means they are serious about their recent push cultivate the open source .NET community.
This is also awesome because it means that creating code analysis tools is much, much easier. Like, an order of magnitude easier. Like, I might even be able to do it. Right now, developers of tools like JetBrains’ ReSharper, Telerik’s JustCode, and even Visual Studio itself had to write their own code that is essentially a duplicate of the existing compiler code. Roslyn allows them to tie into existing operations and make use of the analysis and syntax trees the compiler already has.
If you don’t want to create a full blown productivity extension, anyone can take this and write small extensions that provide new warnings and errors to the compiler. Or create a new refactoring extension that finds duplicate code through an entire solution. Or a tool that finds all comments in your solution and outputs a documentation file. Or create an analysis tool that provides your method’s Kevin Bacon Number!.
Remember, this is just a preview release. Microsoft hasn’t provided a final release date, but I don’t expect it to be anytime soon. For now, go play with it! Look through the code, download the source, have fun! If nothing else, it’s a great look into the C# and Visual Basic compilers.
Now, if you’ll excuse me, I’m going to go add Intellisense to Notepad.