1 / 33

Project Roslyn:

Project Roslyn:. the compiler is at your service. Joe Hummel, PhD @ joehummel joe@joehummel.net http://www.joehummel.net/downloads.html. Chicago Code Camp 2014. Joe Hummel, PhD Professor: U. of Illinois, Chicago Consultant: Joe Hummel, Inc. Trainer: Pluralsight Microsoft MVP C++

ipo
Télécharger la présentation

Project Roslyn:

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Project Roslyn: the compiler is at your service Joe Hummel, PhD @joehummel joe@joehummel.net http://www.joehummel.net/downloads.html 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 Project Roslyn

  3. Demo! 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 class C { . . . } class C { . . . } class C { . . . } csc 000101010101010101010101010 Project Roslyn

  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 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 Project Roslyn

  7. Status • preview release • open source! • http://roslyn.codeplex.com 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 Project Roslyn

  9. Before Roslyn… Project Roslyn

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

  11. After Roslyn… 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! Roslyn csc Project Roslyn

  13. "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 resource strings: foreach(Project p) foreach(Document d) foreach(Resource r) replace(r, r'); csc Project 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 • … Project Roslyn

  15. Compiler Basics… Project Roslyn

  16. Front-end vs. Back-end • Front-end deals with syntax ― "grammar" • Back-end deals with semantics ― "meaning" Project Roslyn

  17. // 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

  18. Roslyn Intermediate Representation (IR) • Abstract Syntax Tree (AST) • Symbol Table + GCD program Project Roslyn

  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 Project Roslyn

  20. Working with Roslyn… Project Roslyn

  21. Roslyn is BIG • There are many APIs… • There is the source code itself… + Project Roslyn

  22. Start small  • Let’s create a simple diagnostic that warns about empty catch blocks… Project Roslyn

  23. Step 1: • Create new project… >> Roslyn >> Diagnostic with Code Fix • Name >> EmptyCatchDiagnostic Project Roslyn

  24. public class DiagnosticAnalyzer: ISyntaxNodeAnalyzer<SyntaxKind> { . . . public ImmutableArray<SyntaxKind> SyntaxKindsOfInterest { get { return ImmutableArray.Create( SyntaxKind.CatchClause); } } // only called for things of interest: public void AnalyzeNode(SyntaxNode node, ...) { varcatchBlock = node as CatchClauseSyntax; if (catchBlock.Block.Statements.Count== 0) // empty! { vardiagnostic = Diagnostic.Create(...); // create warning: addDiagnostic(diagnostic); // display: } } • Step 2: • Create Syntax Node Analyzer to detect empty catches Project Roslyn

  25. internal class CodeFixProvider: ICodeFixProvider { . . // only called for things of interest: public async Task<…> GetFixesAsync(Document document, ...) { varroot = await document.GetSyntaxRootAsync(cancellationToken); vartoken = root.FindToken(span.Start); // catch keyword: if (!token.IsKind(SyntaxKind.CatchKeyword)) // sanity check: return null; varcatchBlock= (CatchClauseSyntax)token.Parent; varthrowStmt = SyntaxFactory.ThrowStatement(); varnewStmts= new SyntaxList<StatementSyntax>().Add(throwStmt); varnewBlock= SyntaxFactory.Block().WithStatements(newStmts); varnewCatchBlock= SyntaxFactory.CatchClause().WithBlock(newBlock). WithAdditionalAnnotations(Formatter.Annotation); varnewRoot= root.ReplaceNode(catchBlock, newCatchBlock); return new[] { CodeAction.Create("throw", document.WithSyntaxRoot(newRoot)) }; } • Step 3: • Create Code Fix Provider to optionally correct problem… Project Roslyn

  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… Project Roslyn

  27. Installing & Using Roslyn… 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" Project Roslyn

  29. Download "End User Preview / SDK Preview" • http://msdn.microsoft.com/en-gb/roslyn • Get the Preview Project Roslyn

  30. Unzip SDK • Install: • End User Preview • Project Templates • Syntax Visualizer • Roslyn Experimental Hive Project Roslyn

  31. Install Microsoft Visual Studio 2013 SDK Project Roslyn

  32. Summary… Project Roslyn

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

More Related