Presentation is loading. Please wait.

Presentation is loading. Please wait.

Joe Hummel, the compiler is at your service Chicago Code Camp 2014.

Similar presentations


Presentation on theme: "Joe Hummel, the compiler is at your service Chicago Code Camp 2014."— Presentation transcript:

1 Joe Hummel, PhD @joehummel joe@joehummel.net http://www.joehummel.net/downloads.html the compiler is at your service Chicago Code Camp 2014

2  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 Chicago Code Camp 2014 2 Project Roslyn

3 Chicago Code Camp 2014 3 Project Roslyn

4  What is Project Roslyn?  The ".NET Compiler Platform"  Replacement of existing.NET compilers with new ones  csc for C#  vbc for VB.NET Chicago Code Camp 2014 4 Project Roslyn 000101010 101010101 010101010 csc

5  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 Chicago Code Camp 2014 5 Project Roslyn

6  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 Chicago Code Camp 2014 6 Project Roslyn

7  Status  preview release  open source!  http://roslyn.codeplex.com http://roslyn.codeplex.com Chicago Code Camp 2014 7 Project Roslyn

8  Open source?  Yes, open source!  Apache license 2.0  You are free to GIT, fork, modify, rebuild, deploy  Anders did this on stage @ Build 2014 Chicago Code Camp 2014 8 Project Roslyn

9 Chicago Code Camp 2014 9 Project Roslyn

10  C# and VB compilers were black boxes  predefined switches only way to interact… Chicago Code Camp 2014 10 Project Roslyn csc > csc.exe main.cs /o /warn:4

11 Chicago Code Camp 2014 11 Project Roslyn

12  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 Chicago Code Camp 2014 Project Roslyn csc Roslyn

13 13 Chicago Code Camp 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

14  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 …… Chicago Code Camp 2014 14 Project Roslyn ? ?

15 Chicago Code Camp 2014 15 Project Roslyn

16  Front-end vs. Back-end  Front-end deals with syntax ― "grammar"  Back-end deals with semantics ― "meaning" Chicago Code Camp 2014 16 Project Roslyn

17 Chicago Code Camp 2014 17 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

18  Roslyn Intermediate Representation (IR)  Abstract Syntax Tree (AST)  Symbol Table Chicago Code Camp 2014 18 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, … ……

19  How to learn Roslyn AST?  Use the Roslyn Syntax Visualizer!  Open a project  Open a source file  View menu… >> Other Windows >> Roslyn Syntax Visualizer Chicago Code Camp 2014 19 Project Roslyn

20 Chicago Code Camp 2014 20 Project Roslyn

21  Roslyn is BIG  There are many APIs…  There is the source code itself… Chicago Code Camp 2014 21 Project Roslyn +

22  Start small  Let’s create a simple diagnostic that warns about empty catch blocks… Chicago Code Camp 2014 22 Project Roslyn

23  Step 1:  Create new project… >> Roslyn >> Diagnostic with Code Fix  Name >> EmptyCatchDiagnostic Chicago Code Camp 2014 23 Project Roslyn

24  Step 2:  Create Syntax Node Analyzer to detect empty catches Chicago Code Camp 2014 24 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: }

25  Step 3:  Create Code Fix Provider to optionally correct problem… Chicago Code Camp 2014 25 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)) }; }

26  Step 4:  Run!  A.vsix installer is built  A new instance of VS is started  The.vsix is installed  Open a project and test… Chicago Code Camp 2014 26 Project Roslyn

27 Chicago Code Camp 2014 27 Project Roslyn

28  Caveats!  You will be *replacing* your C# and VB compilers  Are you sure?  Scripting support is currently missing  i.e. using Roslyn to add C# / VB scripting to your app  MSFT is "re-evaluating design" Chicago Code Camp 2014 28 Project Roslyn

29  Download "End User Preview / SDK Preview" 1.http://msdn.microsoft.com/en-gb/roslynhttp://msdn.microsoft.com/en-gb/roslyn 2.Get the Preview Chicago Code Camp 2014 29 Project Roslyn

30  Unzip SDK  Install: 1.End User Preview 2.Project Templates 3.Syntax Visualizer 4.Roslyn Experimental Hive Chicago Code Camp 2014 30 Project Roslyn

31  Install Microsoft Visual Studio 2013 SDK Chicago Code Camp 2014 31 Project Roslyn

32 Chicago Code Camp 2014 32 Project Roslyn

33  Thank you for attending!  Joe Hummel, PhD  Email: joe@joehummel.netjoe@joehummel.net  Materials: http://www.joehummel.net/downloads.htmlhttp://www.joehummel.net/downloads.html  For more information on Roslyn:  Docs / FAQ:  http://roslyn.codeplex.com/documentation http://roslyn.codeplex.com/documentation  Build 2014 on Channel 9  The Future of C#  https://channel9.msdn.com/Events/Build/2014/2-577 https://channel9.msdn.com/Events/Build/2014/2-577 Chicago Code Camp 2014 33 Project Roslyn


Download ppt "Joe Hummel, the compiler is at your service Chicago Code Camp 2014."

Similar presentations


Ads by Google