Date post: | 06-May-2015 |
Category: |
Technology |
Upload: | microsoft-developer-network-msdn-belgium-and-luxembourg |
View: | 1,355 times |
Download: | 5 times |
Project Roslyn: Exposing the C# and VB compilers’ code analysis
Alex TurnerProgram Manager, VB/C# Languages
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
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
?
DemoA taste of Roslyn…
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
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
Syntax Trees
EfficientResilient
CompleteImmutable
Syntax Tree API – NodesCompilationUnit
TypeDeclaration
MethodDeclaration
class C{ void M() { }}// C▫
ParameterList
Block
var tree = SyntaxTree.ParseCompilationUnit("...");
Syntax Tree API – TokensCompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( ) { }
class C{ void M() { }}// C▫
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▫
Syntax Tree APICompilationUnit
TypeDeclaration
class
C {MethodDeclaratio
n}
EOF
void MParameterLi
stBlock
( ) { }
class C{ void M() { }}// C▫
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);
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▫
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);
CompilationsSyntax
Trees
References
Compilatio
n• Sym
bols• Sem
antic Model
• Flow Analysis
• Diagnostics
• Emit
DemoRoslyn Compiler APIs
Workspace APIHost Environment
Workspace
Solution
Project Project
Document
Document
Solution2 Solutionn
Apply
Edit Edit
SyntaxTree
Compilation
Events (e.g. key presses)
DemoRoslyn Workspace API
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
DemoWriting a Quick Fix
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]
Q&A
© 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.