A Peek Under the Hood of the .NET Compiler Platform – Roslyn

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction

Today’s compilers are magical beings. They transform human written source code into binaries that computers can execute. What happens once the compile command is given at the command line or via the IDE is a mystery to everyone besides the compiler developers.

Developers have gotten used to smart features offered by the IDE like Intellisense, renaming and refactoring support to name a few. These features have just worked for developers, so having knowledge of how they work resides with the developers who build these compilers.

Project Roslyn is Microsoft’s attempt to open up internals of how the .NET compilers operate, by exposing the compiler as a platform and providing developers APIs, which they can use for various code related tasks.

This transition from Microsoft signals a major milestone for platform developers. With Roslyn, platform developers can now build developer tools for Microsoft frameworks – tools related to (i) meta-programming, (ii) code generation and transformation, (iii) interactive use of C# and VB languages, and (iv) embedding VB and C# in domain specific languages.

The .NET Compiler platform, aka Roslyn, exposes two main categories of APIs – compiler APIs (for compiling) and Worksplace APIs (for IDEs).

Compiler Features and Analogous APIs

The classic compiler pipeline includes a few steps. In the table below, we map the .NET compiler platform APIs to the compiler pipeline features.

Compiler Pipeline Feature

Compile API group

Parser

Syntax Tree API

Symbols

Symbol API

Metadata Import

Symbol API

Binder

Binding and Flow Analysis API

IL Emitter

Emit API

Prerequisites

To use Roslyn features, there are a few requisites that need to be present.

1. You need Visual Studio 2013 Professional or a higher variant.

2. Next, you will need to update Nuget Package Manager to the latest version. You can do this from the Tools menu of Visual Studio 2013. Go to Tools -> Extensions and Updates and select Updates. Follow the steps to update the Nuget installation.

3. Next, we need to install the pre-release version of Nuget package “Microsoft.CodeAnalysis”. Open Nuget Package Manager Console and execute the following command. When you execute the command, you will see the output as follows, indicating the installation is successful.

PM> Install-Package Microsoft.CodeAnalysis -Pre
Attempting to resolve dependency 'Microsoft.CodeAnalysis.CSharp.Workspaces (≥ 0.6.4033103-beta)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Workspaces.Common (≥ 0.6.4033103-beta)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.Common (≥ 0.6.4033103-beta)'.
Attempting to resolve dependency 'Microsoft.Bcl.Immutable (≥ 1.1.20-beta)'.
Attempting to resolve dependency 'Microsoft.Bcl.Metadata (≥ 1.0.9-alpha)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.CSharp (≥ 0.6.4033103-beta)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.VisualBasic.Workspaces (≥ 0.6.4033103-beta)'.
Attempting to resolve dependency 'Microsoft.CodeAnalysis.VisualBasic (≥ 0.6.4033103-beta)'.
Installing 'Microsoft.Bcl.Immutable 1.1.20-beta'.
You are downloading Microsoft.Bcl.Immutable from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329769. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.Bcl.Immutable 1.1.20-beta'.
Installing 'Microsoft.Bcl.Metadata 1.0.9-alpha'.
You are downloading Microsoft.Bcl.Metadata from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=329769. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.Bcl.Metadata 1.0.9-alpha'.
Installing 'Microsoft.CodeAnalysis.Common 0.6.4033103-beta'.
You are downloading Microsoft.CodeAnalysis.Common from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=394369. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Common 0.6.4033103-beta'.
Installing 'Microsoft.CodeAnalysis.Workspaces.Common 0.6.4033103-beta'.
You are downloading Microsoft.CodeAnalysis.Workspaces.Common from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=394369. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.Workspaces.Common 0.6.4033103-beta'.
Installing 'Microsoft.CodeAnalysis.CSharp 0.6.4033103-beta'.
You are downloading Microsoft.CodeAnalysis.CSharp from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=394369. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.CSharp 0.6.4033103-beta'.
Installing 'Microsoft.CodeAnalysis.CSharp.Workspaces 0.6.4033103-beta'.
You are downloading Microsoft.CodeAnalysis.CSharp.Workspaces from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=394369. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.CSharp.Workspaces 0.6.4033103-beta'.
Installing 'Microsoft.CodeAnalysis.VisualBasic 0.6.4033103-beta'.
You are downloading Microsoft.CodeAnalysis.VisualBasic from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=394369. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.VisualBasic 0.6.4033103-beta'.
Installing 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 0.6.4033103-beta'.
You are downloading Microsoft.CodeAnalysis.VisualBasic.Workspaces from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=394369. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 0.6.4033103-beta'.
Installing 'Microsoft.CodeAnalysis 0.6.4033103-beta'.
You are downloading Microsoft.CodeAnalysis from Microsoft, the license agreement to which is available at http://go.microsoft.com/fwlink/?LinkId=394369. Check the package for additional dependencies, which may come with their own license agreement(s). Your use of the package and dependencies constitutes your acceptance of their license agreements. If you do not accept the license agreement(s), then delete the relevant components from your device.
Successfully installed 'Microsoft.CodeAnalysis 0.6.4033103-beta'.
Adding 'Microsoft.Bcl.Immutable 1.1.20-beta' to ConsoleApplication1.
Successfully added 'Microsoft.Bcl.Immutable 1.1.20-beta' to ConsoleApplication1.
Adding 'Microsoft.Bcl.Metadata 1.0.9-alpha' to ConsoleApplication1.
Successfully added 'Microsoft.Bcl.Metadata 1.0.9-alpha' to ConsoleApplication1.
Adding 'Microsoft.CodeAnalysis.Common 0.6.4033103-beta' to ConsoleApplication1.
Successfully added 'Microsoft.CodeAnalysis.Common 0.6.4033103-beta' to ConsoleApplication1.
Adding 'Microsoft.CodeAnalysis.Workspaces.Common 0.6.4033103-beta' to ConsoleApplication1.
Successfully added 'Microsoft.CodeAnalysis.Workspaces.Common 0.6.4033103-beta' to ConsoleApplication1.
Adding 'Microsoft.CodeAnalysis.CSharp 0.6.4033103-beta' to ConsoleApplication1.
Successfully added 'Microsoft.CodeAnalysis.CSharp 0.6.4033103-beta' to ConsoleApplication1.
Adding 'Microsoft.CodeAnalysis.CSharp.Workspaces 0.6.4033103-beta' to ConsoleApplication1.
Successfully added 'Microsoft.CodeAnalysis.CSharp.Workspaces 0.6.4033103-beta' to ConsoleApplication1.
Adding 'Microsoft.CodeAnalysis.VisualBasic 0.6.4033103-beta' to ConsoleApplication1.
Successfully added 'Microsoft.CodeAnalysis.VisualBasic 0.6.4033103-beta' to ConsoleApplication1.
Adding 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 0.6.4033103-beta' to ConsoleApplication1.
Successfully added 'Microsoft.CodeAnalysis.VisualBasic.Workspaces 0.6.4033103-beta' to ConsoleApplication1.
Adding 'Microsoft.CodeAnalysis 0.6.4033103-beta' to ConsoleApplication1.
Successfully added 'Microsoft.CodeAnalysis 0.6.4033103-beta' to ConsoleApplication1.
 
PM>

Now, our development environment is ready to use to play with the .NET Compiler platform. In upcoming articles, we will dive deep into the .NET compiler platform.

Summary

In this article, we got an introduction of Roslyn, the .NET compiler platform. Watch for more articles where we will dig deeper into each API of Rolsyn in more detail.

About the Author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read