project roslyn: exposing the c# and vb compiler’s code analysis

23
Project Roslyn: Exposing the C# and VB compilers’ code analysis Alex Turner Program Manager, VB/C# Languages

Upload: microsoft-developer-network-msdn-belgium-and-luxembourg

Post on 06-May-2015

1.361 views

Category:

Technology


5 download

DESCRIPTION

More info on http://www.techdays.be

TRANSCRIPT

Page 1: Project Roslyn: Exposing the C# and VB compiler’s code analysis

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

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

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

DemoA taste of Roslyn…

Page 5: Project Roslyn: Exposing the C# and VB compiler’s code analysis

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

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

Syntax Trees

EfficientResilient

CompleteImmutable

Page 8: Project Roslyn: Exposing the C# and VB compiler’s code analysis

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

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

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

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

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

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

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

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

DemoRoslyn Compiler APIs

Page 17: Project Roslyn: Exposing the C# and VB compiler’s code analysis

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

DemoRoslyn Workspace API

Page 19: Project Roslyn: Exposing the C# and VB compiler’s code analysis

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

DemoWriting a Quick Fix

Page 21: Project Roslyn: Exposing the C# and VB compiler’s code analysis

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

Q&A

Page 23: Project Roslyn: Exposing the C# and VB compiler’s code analysis

© 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.