This project is read-only.

Are there any plans to create API specifically for code generation?

Topics: APIs
Aug 31, 2014 at 2:00 PM
Are there any plans to create API specifically for code generation?
Now code looks terrible:
For example string constant creation:
var constField = SyntaxFactory.FieldDeclaration(
                    SyntaxFactory.TokenList(SyntaxFactory.Token(visibility), SyntaxFactory.Token(SyntaxKind.ConstKeyword)),
                                        SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Literal(value))))
CodeDom is much more simple, but it don't allow to make some lang constructions like 'switch'.

Function generation looks even more terrible :)
Sep 1, 2014 at 12:04 AM
Edited Sep 1, 2014 at 5:36 AM
I agree.
It would be much easier to have a template like feature. In my examples below i extend function syntax.

template Syntax<Language> Const<Language, T>( string name , T value ) 
 return $("const {$name} = {$value};"); /* String Interpolation syntax */
Template Function  Const(Of Language, T)( name As String, value As T ) As Syntax<Language>
 return $("const {$name} = {$value};") ' String Interpolation syntax
End Function
var constnode = Const<Language.CSharp, String>( "Name", "Rosly")

Creation of Dim x = 0
template function [Dim]<T>( id : String!, Value : T) : SyntaxNode<VB>
 return $("Dim {$id} = {$Value){\n}")
end template
In use: Dim code = [Dim]( "x", 0 )

template function VB6_CodeSmell ( input : Syntax<VB> ) : Syntax<VB>
  var m  = SyntaxNode<VB>.CreateMatcher( $("Dim {$name} As {$type}{\n}{$name} = {$value}{\n}"))
  if( m == input )
    var output = SyntaxNode(Of VB).Create( $("Dim {$m("name")} As {$m("type")} = {$m("value")}{\n}")
    return output
   return input
They return a syntax tree, by exploiting the roslyn compiler to auto-magically generate the syntax node for you.
Akin to how using iterator prefix on a function signature auto-synthesizes an iterator for you.

Generate IF
template function SingleLine_IF( pred : Expr<Bool>, value : Syntax<C#> ): Syntax<C#>
  return Syntax<C#>.Create( $("if($pred) return {$value};") );

template function [IfElse]( pred : Expr<Bool>, true_expr : Syntax<C#>, false_expr : Syntax ) : Syntax<C#>
  return Syntax<C#>.Create($("if($pred){{$true_expr}} else {{ {$false_expr} }}"); 

You could also have an "inlining" of Any.

template function [Comment] ( Text : String! ) : Syntax<C#>
  return $("/* {$Text} */")(Syntax<C#>);

template function Any<T> ( source : Expr<ICollection<T>> , pred : Expr<T,Boolean> ) : Syntax<C#>
 return $("int _lim = {$ Source.Count };")(Syntax<C#>) + 
  $("for( int index = 0; index < _lim; index++ )")(Syntax<C#>) +
  $("{{")(Syntax<C#>) + [Comment]("Check the predicate")
  $("if( {$pred}(index) ) {{ return true; }}")(Syntax<C#>)
  $("}}")(Syntax<C#>) +
  $("return false;")(Syntax<C#>)
Sep 3, 2014 at 2:28 PM
If I'm not mistaken, RoslynDOM is being built partially for this purpose by forum regular and code generation MVP Kathleen Dollard. It looks to be about the right distance from icky strings and icky War and Peace-length invocations.
Sep 5, 2014 at 5:28 PM
You have T4 Templates, which are pretty usefull and dynamic enough to create a powerfull code generator...
Sep 6, 2014 at 12:27 PM
We use Roslyn for generating code, that is not changed frequently. Roslyn is much more usefull, because it can split generated code to many files instead of one for T4.
By MSBuildWorkspace.Create() ->load solution-> modify project in solution.

For example you can write 'protocol definition' and generate all needed data classes
Sep 6, 2014 at 8:47 PM
Hey all,

In answer to your question. Yes, there are plans and we're working on it. I don't have any dates to announce yet. Code generation, particularly with some language agnostic abstraction, is very valuable to developers writing code fixes or refactorings and right now that's the scenario we're investing in supporting.

I'll let you know when we have any updates.


Anthony D. Green, Program Manager, Visual Basic & C# Languages Team