Expose lowering support

Topics: APIs
Apr 28, 2014 at 8:21 PM
I understand this happens on the bound tree and that keeping that tree internal is helpful so the Roslyn API is not locked down in that area, but it would be really nice to have some lowering support exposed on the public syntax tree. If I want to use Roslyn to make a cross-compiler to JavaScript, for example, I would need to manually lower certain higher-level constructs that you are already doing to generate IL.

For example, re-writing LINQ queries in the fluent method syntax, or taking an iterator block and turning it into a state machine class. It would be great if we could take a parsed syntax tree, call Lower() on it (perhaps with some options as to what should be lowered), and get back a semantically equivalent syntax tree that has no higher-level constructs in it.
Developer
Apr 30, 2014 at 4:48 PM
We lower to constructs that simply do not exist in the source language, so there is in general no way for us to produce a lowered source. In addition, we do not lower by performing source transformations, so producing lowered source, even if possible, would be a significant work item extending what is currently implemented in Roslyn.

Having said all that, we are considering exposing a control-flow-graph data structure representing a method body. That might prove suitable for a starting point for a new code generator.
Apr 30, 2014 at 7:07 PM
Thank you for the response. Yes, I understand that this is a pretty big request. I also understand its not possible in the general sense, so perhaps a Lower() method would not be the appropriate way to expose functionality like this, but it might be nice if the API had some publicly exposed rewriters which could be used to re-write a LINQ query as method calls, an extension method invocation as a static method invocation, an auto-implemented property as a normal property and field, ...

It seems like some refactorings/code fixes might benefit from this functionality. I can see why iterator block re-writing might not be something worth the effort though. The control flow graph seems interesting. I look forward to seeing that if it is exposed.