Joe Hummel, the compiler is at your service SDC Meetup, Sept 2014
Joe Hummel, PhD Professor:U. of Illinois, Chicago Consultant:Joe Hummel, Inc. Trainer:Pluralsight Microsoft MVP C++ Chicago-based, one daughter adopted from China (now 12!) Avid sailor SDC Meetup Sept Project Roslyn
SDC Meetup Sept Project Roslyn
What is Project Roslyn? The ".NET Compiler Platform" Replacement of existing.NET compilers with new ones csc for C# vbc for VB.NET SDC Meetup Sept Project Roslyn csc
Why is this a big deal? Risky if they get this wrong, folks can't build their apps if they get this wrong, MSFT can't build their apps SDC Meetup Sept Project Roslyn
What's the benefit? Faster turnaround on new features inside and outside MSFT Grow the Visual Studio ecosystem MUCH easier to build new tools MUCH easier to extend Visual Studio, C# and VB MUCH easier to try out new ideas SDC Meetup Sept Project Roslyn
Status preview release open source! SDC Meetup Sept Project Roslyn
Open source? Yes, open source! Apache license 2.0 You are free to GIT, fork, modify, rebuild, deploy Anders did this on Build 2014 SDC Meetup Sept Project Roslyn
SDC Meetup Sept Project Roslyn
C# and VB compilers were black boxes predefined switches only way to interact… SDC Meetup Sept Project Roslyn csc > csc.exe main.cs /o /warn:4
SDC Meetup Sept Project Roslyn
The compilers are now white boxes You can: obtain information about a program modify a program syntactically / semantically impact the compilation process change the compiler itself! 12 SDC Meetup Sept 2014 Project Roslyn csc Roslyn
13 SDC Meetup Sept 2014 Project Roslyn csc "Call me every time you see an identifier…" (because I'm renaming all global variables) "Emit this code instead…" (I'm targeting specific HW) // translate resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace (r, r'); // translate resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace (r, r'); Roslyn
What can we do with this capability? Infinite possibilities: better tools — refactoring, analysis, … better enforcement of coding standards add scripting support to your app target new platforms language research — DSLs, … compiler research …… SDC Meetup Sept Project Roslyn ? ?
SDC Meetup Sept Project Roslyn
Front-end vs. Back-end Front-end deals with syntax ― "grammar" Back-end deals with semantics ― "meaning" SDC Meetup Sept Project Roslyn
SDC Meetup Sept Project Roslyn Source language Parsing Assembly language Lexical Analysis Compiler Semantic Analysis High-level Optimizer Code Gen Low-level Optimizer tokens IRIR'IR'' IR''' // comment if (x>100) x = 100; // comment if (x>100) x = 100; if, (, x, >, 100, ), x, =, … syntax errors semantic errors
Roslyn Intermediate Representation (IR) Abstract Syntax Tree (AST) Symbol Table SDC Meetup Sept Project Roslyn + GCD program 0"int", type, … 1"void", type, … 2… 3"getint", funct, type: 0, … 4"putint", funct, type: 1, … 5"i", var, type: 0, … 6"j", var, type: 0, … ……
How to learn Roslyn AST? Use the Roslyn Syntax Visualizer! Open a project Open a source file View menu… >> Other Windows >> Roslyn Syntax Visualizer SDC Meetup Sept Project Roslyn
SDC Meetup Sept Project Roslyn
Roslyn is BIG There are many APIs… There is the source code itself… SDC Meetup Sept Project Roslyn +
Start small Let’s create a simple diagnostic that warns about empty catch blocks… SDC Meetup Sept Project Roslyn
Step 1: Create new project… >> Roslyn >> Diagnostic with Code Fix Name >> EmptyCatchDiagnostic SDC Meetup Sept Project Roslyn
Step 2: Create Syntax Node Analyzer to detect empty catches SDC Meetup Sept Project Roslyn public class DiagnosticAnalyzer : ISyntaxNodeAnalyzer {. public ImmutableArray SyntaxKindsOfInterest { get { return ImmutableArray.Create( SyntaxKind.CatchClause ); } } // only called for things of interest: public void AnalyzeNode(SyntaxNode node,...) { var catchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count == 0) // empty! { var diagnostic = Diagnostic.Create(...); // create warning: addDiagnostic(diagnostic); // display: } public class DiagnosticAnalyzer : ISyntaxNodeAnalyzer {. public ImmutableArray SyntaxKindsOfInterest { get { return ImmutableArray.Create( SyntaxKind.CatchClause ); } } // only called for things of interest: public void AnalyzeNode(SyntaxNode node,...) { var catchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count == 0) // empty! { var diagnostic = Diagnostic.Create(...); // create warning: addDiagnostic(diagnostic); // display: }
Step 3: Create Code Fix Provider to optionally correct problem… SDC Meetup Sept Project Roslyn internal class CodeFixProvider : ICodeFixProvider {. // only called for things of interest: public async Task GetFixesAsync(Document document,...) { var root = await document.GetSyntaxRootAsync(cancellationToken); var token = root.FindToken(span.Start); // catch keyword: if (!token.IsKind(SyntaxKind.CatchKeyword)) // sanity check: return null; var catchBlock = (CatchClauseSyntax)token.Parent; var throwStmt = SyntaxFactory.ThrowStatement(); var newStmts = new SyntaxList ().Add(throwStmt); var newBlock = SyntaxFactory.Block().WithStatements(newStmts); var newCatchBlock = SyntaxFactory.CatchClause(). WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); var newRoot = root.ReplaceNode(catchBlock, newCatchBlock); return new[] { CodeAction.Create("throw", document.WithSyntaxRoot(newRoot)) }; } internal class CodeFixProvider : ICodeFixProvider {. // only called for things of interest: public async Task GetFixesAsync(Document document,...) { var root = await document.GetSyntaxRootAsync(cancellationToken); var token = root.FindToken(span.Start); // catch keyword: if (!token.IsKind(SyntaxKind.CatchKeyword)) // sanity check: return null; var catchBlock = (CatchClauseSyntax)token.Parent; var throwStmt = SyntaxFactory.ThrowStatement(); var newStmts = new SyntaxList ().Add(throwStmt); var newBlock = SyntaxFactory.Block().WithStatements(newStmts); var newCatchBlock = SyntaxFactory.CatchClause(). WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); var newRoot = root.ReplaceNode(catchBlock, newCatchBlock); return new[] { CodeAction.Create("throw", document.WithSyntaxRoot(newRoot)) }; }
Step 4: Run! A.vsix installer is built A new instance of VS is started The.vsix is installed Open a project and test… SDC Meetup Sept Project Roslyn
SDC Meetup Sept Project Roslyn
Install Roslyn preview on top of VS 2013… That supports seems to be gone Roslyn preview ships as part of VS “14” CTP 3 So just install VS 14, or Run pre-built VS 14 VM available in Azure VM gallery See for more detailshttp://roslyn.codeplex.com/ SDC Meetup Sept Project Roslyn
SDC Meetup Sept Project Roslyn
Thank you for attending! Joe Hummel, PhD Materials: For more information on Roslyn: Docs / FAQ: Build 2014 on Channel 9 The Future of C# SDC Meetup Sept Project Roslyn