project roslyn: exposing the c# and vb compiler’s code analysis
DESCRIPTION
More info on http://www.techdays.beTRANSCRIPT
![Page 1: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/1.jpg)
Project Roslyn: Exposing the C# and VB compilers’ code analysis
Alex TurnerProgram Manager, VB/C# Languages
![Page 2: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/2.jpg)
Session Contents• Articulate the purpose, scope and goals of Project
Roslyn• Build rich "code aware" tools and Visual Studio
extensions for C# and Visual Basic• Navigate and explain the large set of compiler APIs
exposed by Project Roslyn
![Page 3: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/3.jpg)
MICROSOFT CONFIDENTIAL – INTERNAL ONLY
Codename Roslyn(formerly known as compiler-as-a-service)
• Managed compilers + language services• Code analysis APIs• Scripting APIs• Language service extensibility• Read-Eval-Print-Loop (REPL)
• Ship date:• v.Next + n, where n > 0
?
![Page 4: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/4.jpg)
DemoA taste of Roslyn…
![Page 5: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/5.jpg)
Symbol API
Roslyn APIsLanguage Service
Compiler API
Compiler Pipeline
Syntax Tree API
Binding and Flow Analysis APIs
Emit API
Form
atte
r
Colo
rizer
Ou
tlinin
g
Navig
ate
To
Ob
ject
Bro
wse
r
Com
ple
tion
List
Find
All
Refe
ren
ces
Ren
am
e
Qu
ick Info
Sig
natu
re
Help
Extra
ct M
eth
od
Go To
D
efin
ition
Ed
it an
d
Con
tinu
e
ParserMetadata
Import
Binder IL EmitterSymbols
![Page 6: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/6.jpg)
Symbol API
Roslyn API Layers
Services
Compiler Syntax Tree API
Binding and Flow Analysis
APIs
Emit API
Scripting API
Workspace API
Code FormattingFind All
ReferencesName
Simplification …
Editor Services
Code Actions Classification Completion …Outlining
![Page 7: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/7.jpg)
Syntax Trees
EfficientResilient
CompleteImmutable
![Page 8: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/8.jpg)
Syntax Tree API – NodesCompilationUnit
TypeDeclaration
MethodDeclaration
class C{ void M() { }}// C▫
ParameterList
Block
var tree = SyntaxTree.ParseCompilationUnit("...");
![Page 9: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/9.jpg)
Syntax Tree API – TokensCompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( ) { }
class C{ void M() { }}// C▫
![Page 10: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/10.jpg)
Syntax Tree API – “Trivia”CompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( )
{ }
SP EOL EOL // C
SPx4 SP
EOL
EOL
EOLSPx4 EOL SPx4
class∙C{∙∙∙∙void∙M()∙∙∙∙{∙∙∙∙}}// C▫
![Page 11: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/11.jpg)
Syntax Tree APICompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( ) { }
class C{ void M() { }}// C▫
![Page 12: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/12.jpg)
Syntax Tree APICompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( ) { }
class C{ void M(int x) { }}// C▫
var oldList = method.ParameterList;var newList = oldList.Update( openParenToken: oldList.OpenParenToken, parameters: Syntax.SeparatedList( Syntax.Parameter( typeOpt: Syntax.ParseTypeName("int "), identifier: Syntax.Identifier("x"))), closeParenToken: oldList.CloseParenToken);
var newRoot = tree.Root.ReplaceNode(oldList, newList);
var oldList = method.ParameterList;var newList = oldList.Update( openParenToken: oldList.OpenParenToken, parameters: Syntax.ParseParameterList("(int x)").Parameters, closeParenToken: oldList.CloseParenToken);
var newRoot = tree.Root.ReplaceNode(oldList, newList);
![Page 13: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/13.jpg)
Syntax Tree API
var oldList = method.ParameterList;var newList = oldList.Update( openParenToken: oldList.OpenParenToken, parameters: Syntax.ParseParameterList("(int x)").Parameters, closeParenToken: oldList.CloseParenToken);
var newRoot = tree.Root.ReplaceNode(oldList, newList);
CompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( ) { }Parameter
int
xPredefinedTyp
e
class C{ void M(int x) { }}// C▫
![Page 14: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/14.jpg)
Syntax Tree APICompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( ) { }Parameter
int
xPredefinedTyp
e
class C{ void M(int x) { }}// C▫
var oldList = method.ParameterList;var newList = oldList.Update( openParenToken: oldList.OpenParenToken, parameters: Syntax.ParseParameterList("(int x)").Parameters, closeParenToken: oldList.CloseParenToken);
var newRoot = tree.Root.ReplaceNode(oldList, newList);
![Page 15: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/15.jpg)
CompilationsSyntax
Trees
References
Compilatio
n• Sym
bols• Sem
antic Model
• Flow Analysis
• Diagnostics
• Emit
![Page 16: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/16.jpg)
DemoRoslyn Compiler APIs
![Page 17: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/17.jpg)
Workspace APIHost Environment
Workspace
Solution
Project Project
Document
Document
Solution2 Solutionn
Apply
Edit Edit
SyntaxTree
Compilation
Events (e.g. key presses)
![Page 18: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/18.jpg)
DemoRoslyn Workspace API
![Page 19: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/19.jpg)
Code Actions API A representation of a code changeCode Action
• Produces both an edit and a preview of that edit
An interesting span of source codeCode Issue
• Called for every node, token or trivia in a source file• Optional: May be squiggled in the editor and shown in the Error List• Optional: May provide one or more Code Actions
A contextually-available Code ActionCode Refactoring
• Operates on the current selection in the editor• Must provide one or more Code Actions
![Page 20: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/20.jpg)
DemoWriting a Quick Fix
![Page 21: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/21.jpg)
Be what’s next• Install the Roslyn CTP• Try out the APIs• Give us feedback!
Dev Center: http://msdn.microsoft.com/nl-be/roslynForum: http://social.msdn.microsoft.com/forums/nl-be/roslyn
Email: [email protected]
![Page 22: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/22.jpg)
Q&A
![Page 23: Project Roslyn: Exposing the C# and VB compiler’s code analysis](https://reader035.vdocuments.us/reader035/viewer/2022062319/554a2dcdb4c9051b578b4f6f/html5/thumbnails/23.jpg)
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.