Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Compiler Plugins (with working implementation) #3248

Closed
russpowers opened this issue Jun 2, 2015 · 3 comments
Closed

Proposal: Compiler Plugins (with working implementation) #3248

russpowers opened this issue Jun 2, 2015 · 3 comments

Comments

@russpowers
Copy link

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 transform Compilation 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 implements INotifyPropertyChanged 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.

@davidroth
Copy link

Hi, nice work - thanks for sharing :)
I think you can use #line pre-processor directives to improve debugging the processed assemblies:
http://blogs.msdn.com/b/abhinaba/archive/2005/10/10/479016.aspx

One question regarding the CscToolPath: Does this mean that VS 2015 will start an additional "VBCSCCompiler.exe" process which gets re-used or will every rebuild create an entire new roslyn build process?

@russpowers
Copy link
Author

Thanks! I didn't know about the #line directive, I will try it out. The CscToolPath causes a single VBCSCompiler.exe to start up when you first compile (which takes a couple of seconds), but it reuses that for subsequent builds.

@gafter
Copy link
Member

gafter commented Nov 20, 2015

We are in progress in #5561 on a feature similar to this.

@gafter gafter removed this from the C# 7 and VB 15 milestone Nov 20, 2015
@gafter gafter added this to the 2.0 milestone Dec 15, 2015
@gafter gafter closed this as completed Dec 15, 2015
@gafter gafter added the Resolution-Duplicate The described behavior is tracked in another issue label Dec 15, 2015
@gafter gafter added the Feature - Replace/Original Replace/Original label Apr 27, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants