Proposal: Compiler Plugins (with working implementation) #3248
Labels
Area-Compilers
Area-Language Design
Discussion
Feature - Replace/Original
Replace/Original
Feature Request
Resolution-Duplicate
The described behavior is tracked in another issue
Milestone
Hi everyone, I've been working on an experiment with Roslyn that I'd like to share. I've always thought that Roslyn would be great for compile-time metaprogramming, but it generally seems to be pretty far down on the priority list. I'd like to try to change that. Using ideas from ASP.NET DNX's
ICompileModule
, I have created a way to write compiler plugins that transformCompilation
objects (including their syntax trees) as part of the Roslyn compilation process. It's designed to be as simple and flexible as possible, and users can easily activate plugins for a project by adding an assembly reference to the plugin and applying its assembly attribute.My fork is located at https://github.com/russpowers/roslyn. The relevant commit is here. Please see this readme for a more in-depth explanation of how things work.
I have also implemented a basic attribute-based macro system which uses the new compiler plugin architecture at https://github.com/russpowers/MacroSharp. It is working with the Visual Studio RC, just follow the instructions in the readme to get it set up.
MacroCompiler.cs and MacroPluginAttribute.cs are the files that directly hook in to Roslyn as a plugin. An example of the
MacroPlugin
getting loaded using an assembly attribute is here. There is a single example right now that automatically implementsINotifyPropertyChanged
using an attribute macro here. It's rough, but it works to demonstrate the idea.This all currently works in Visual Studio RC for compilation using a modified
CscToolPath
, but intellisense and refactoring use the original untransformed syntax. Debugging mostly works, but locations get messed up if the syntax trees are changed too much. Once OpenSourceDebug is all sync'd up again, I will try it out with Visual Studio intellisense (although I anticipate there may be some issues).I have not submitted a pull request for this, since I'm not exactly an expert on Roslyn, and I'm sure I've made some mistakes or invalid assumptions. Please take a look at my code and let me know if you have any questions or feedback. Hopefully we can use this as a starting point for getting some compile-time metaprogramming support into Roslyn.
The text was updated successfully, but these errors were encountered: