400 likes | 664 Views
Project Roslyn:. the compiler is at your service. Joe Hummel, PhD @ joehummel joe@joehummel.net http://www.joehummel.net/downloads.html. Chicago Coder Conference, June 2016. Joe Hummel, PhD Professor: U. of Illinois, Chicago Consultant: Joe Hummel, Inc. Trainer: Pluralsight
E N D
Project Roslyn: the compiler is at your service Joe Hummel, PhD @joehummel joe@joehummel.net http://www.joehummel.net/downloads.html Chicago Coder Conference, June 2016
Joe Hummel, PhD • Professor: U. of Illinois, Chicago • Consultant: Joe Hummel, Inc. • Trainer: Pluralsight • Microsoft MVP Visual C++ • Chicago-based, one daughter adopted from China (now 14!) • Avid Lake Michigan sailor Project Roslyn
Demo! Project Roslyn
What is Project Roslyn? • The ".NET Compiler Platform" • Replacement of previous .NET compilers with new ones • csc for C# • vbc for VB.NET class C { . . . } class C { . . . } class C { . . . } csc 000101010101010101010101010 Project Roslyn
Risky • if Microsoft gets this wrong, they break a lot of code --- including their own Project Roslyn
So why did Microsoft do this? • Open up the compiler • Now folks can… • Extend C# and VB with new features • Target other platforms --- e.g. Raspberry PI? • Take advantage of the rich information the compiler has about programs --- e.g. better refactoring, analysis, testing? 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 Project Roslyn
Status • Integrated into Visual Studio 2015 • Additional .NET Compiler Platform SDK • Shipped as a VS extension • https://github.com/dotnet/roslyn Project Roslyn
Open source? • Yes, open source! • Apache license 2.0 • You are free to GIT, fork, modify, rebuild, deploy • Anders released on stage @ Build 2014 Project Roslyn
Before Roslyn… Project Roslyn
C# and VB compilers were black boxes • predefined switches only way to interact… > csc.exe main.cs /o /warn:4 csc Project Roslyn
After Roslyn… 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! Roslyn csc Project Roslyn
Roslyn APIs Project Roslyn
"Call me every time you see an identifier…" (because I'm renaming all global variables) "Emit this code instead…" (I'm targeting specific HW) Roslyn // translate project resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace(r, r'); csc Project Roslyn
Many of the features in Visual Studio are driven by Roslyn… Project Roslyn
? • What can we do with this capability? • Infinite possibilities: • better tools — refactoring, analysis, … • better enforcement of coding standards • integrate C# / VB into your app • target new platforms • language research — DSLs, … • compiler research • … Project Roslyn
Compiler Basics… Project Roslyn
Front-end vs. Back-end • Front-end deals with syntax ― "grammar" • Back-end deals with semantics ― "meaning" Project Roslyn
// comment if (x>100) x = 100; Typical Compiler Phaseslexical analysisparsingsemantic analysisHL optimizercode genLL optimizer Sourcelanguage Lexical Analysis Compiler tokens if, (, x, >, 100, ), x, =, … IR IR' IR'' Parsing Semantic Analysis High-level Optimizer Code Gen IR''' Low-level Optimizer syntaxerrors semanticerrors Assemblylanguage Project Roslyn
Roslyn Intermediate Representation (IR) • Abstract Syntax Tree (AST) • Symbol Table + GCD program Project Roslyn
How to learn Roslyn AST? • Use the Roslyn Syntax Visualizer! • Install .NET Compiler Platform SDK • Open a project • Open a source file • View menu… >> Other Windows >> Syntax Visualizer Project Roslyn
Working with Roslyn… Project Roslyn
Roslyn is BIG • There are many APIs… • There is the source code itself… + Project Roslyn
Start small • Let’s create a simple diagnostic that warns about empty catch blocks… Project Roslyn
Step 1: • Install .NET Compiler Platform SDK • Create new project… >> Extensibility >> Diagnostic with Code Fix • Name >> EmptyCatchDiagnostic ` Project Roslyn
public class CatchBlockDiagnosticAnalyzer: DiagnosticAnalyzer { . . . public override void Initialize(AnalysisContext context) { context.RegisterSyntaxNodeAction<SyntaxKind>(AnalyzeNode, SyntaxKind.CatchClause); } // only called for things of interest: public void AnalyzeNode(SyntaxNodeAnalysisContext context) { var node = context.Node; varcatchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count== 0) // empty! { vardiagnostic = Diagnostic.Create(...); // create warning: context.ReportDiagnostic(diagnostic); // display: } } • Step 2: • Create Analyzer to detect empty catch blocks Project Roslyn
internal class CatchBlockDiagnosticCodeFixProvider: CodeFixProvider { // only called for things of interest: public … async Task RegisterCodeFixesAsync(CodeFixContext context) { varroot = await context.Document.GetSyntaxRootAsync(…); vardiagnostic = context.Diagnostics.First(); vardiagnosticSpan = diagnostic.Location.SourceSpan; var catch = root.FindToken(…).Parent.AncestorsAndSelf(). OfType<CatchClauseSyntax>.First(); context.RegisterCodeFix(CodeAction.Create("throw", c => UpdateCatchBlock(context.Document, catch, c)), diagnostic); } private async Task<Document> UpdateCatchBlock(Document doc, …) { varthrowStmt = SyntaxFactory.ThrowStatement(); varnewStmts= new SyntaxList<StatementSyntax>().Add(throwStmt); varnewBlock= SyntaxFactory.Block().WithStatements(newStmts); varnewCatchBlock= SyntaxFactory.CatchClause().WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); varroot= await document.GetSyntaxRootAsync(); varnewRoot= root.ReplaceNode(catchBlock, newCatchBlock); varnewDocument= document.WithSyntaxRoot(newRoot); return newDocument; } • Step 3: • Create Code Fix Provider to optionally correct problem… Project Roslyn
Step 4: • Run! • A .vsix installer is built • A new instance of VS is started • The .vsix is installed • Open a project and test… Project Roslyn
Installing Roslyn… Project Roslyn
Roslyn compilers are part of Visual Studio 2015 • You’ll also want .NET Compiler Platform SDK • Visual Studio extension • Tools >> Updates and Extensions Project Roslyn
Summary… Project Roslyn
Thank you for attending! • Joe Hummel, PhD • Email: joe@joehummel.net • Materials: http://www.joehummel.net/downloads.html • For more information on Roslyn: • MSDN: • “C# - Adding a Code Fix to Your Roslyn Analyzer”, by Alex Turner, Feb 2015 • Docs / FAQ: • https://github.com/dotnet/roslyn • https://github.com/dotnet/Roslyn/wiki/Roslyn%20Overview • Microsoft’s Channel 9 Project Roslyn