the microsoft compiler platform (roslyn). a quick overview

26
The Microsoft Compiler Platfor Johnny Hooyberghs [email protected] @djohnnieke

Upload: johnny-hooyberghs

Post on 09-Aug-2015

236 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: The Microsoft Compiler Platform (Roslyn). A quick overview

The Microsoft Compiler PlatformJohnny Hooyberghs

[email protected]

@djohnnieke

Page 2: The Microsoft Compiler Platform (Roslyn). A quick overview

New events in the coming months

• May: no event• June

• Dennis Doomen (Netherlands)• Session TBA

• September• Christos Matskas (UK)• Session TBA

• October• You?

Page 4: The Microsoft Compiler Platform (Roslyn). A quick overview

The Microsoft Compiler PlatformJohnny Hooyberghs

[email protected]

@djohnnieke

Page 5: The Microsoft Compiler Platform (Roslyn). A quick overview

I work for Involved-IT

A group of passionate software engineers who design, build and deliver software using .NET and the best bits of the Web

Page 6: The Microsoft Compiler Platform (Roslyn). A quick overview

agenda• Roslyn Overview• New C# 6 language features• Compiler API• syntax, compilation, symbols, data flow,

control flow

• Diagnostics and code fixes• Analyzers and NuGet• Scripting

Page 7: The Microsoft Compiler Platform (Roslyn). A quick overview

The .NET Compiler Platform ("Roslyn") provides open-source C# and Visual Basic compilers with rich

code analysis APIs

It enables building code analysis tools with the same APIs that are

used by Visual Studio

Page 8: The Microsoft Compiler Platform (Roslyn). A quick overview

roslyn overview• Rewrite of the VB and C# compilers• Written in C# and VB itself• Language services integrated in IDE• Intellisense• Code browsing experiences• Refactorings• ...

• Code analysis from managed languages• Build compilers and tools

Page 9: The Microsoft Compiler Platform (Roslyn). A quick overview

why roslyn• Managed languages• Better productivity• New language features are easier to

implement• 1 compiler, different contexts• csc.exe, vbc.exe• Background compilers in IDE• Snippet compiler

• Support for experimentation and prototyping• Richer tooling, 3rd party extensions,

education

Page 10: The Microsoft Compiler Platform (Roslyn). A quick overview

current compiler

compilerC#VB

IL

Page 11: The Microsoft Compiler Platform (Roslyn). A quick overview

current compiler

parser symbols binder IL emitter

C#VB

IL

Page 12: The Microsoft Compiler Platform (Roslyn). A quick overview

roslyn compiler

C#VB

ILSynta

x Tree API

Symbo

l API

Binding & Flow

Analysis API

Emit API

Page 13: The Microsoft Compiler Platform (Roslyn). A quick overview

By rewriting the C# and VB compiler it becomes easier to implement new

language features

Smaller language features are finally worked on because of the smaller

impact

Page 14: The Microsoft Compiler Platform (Roslyn). A quick overview

C# history

C# 1Managed

C# 2Generics

C# 3LINQ

C# 4Dynamic

programing

C# 5Asynchronous programing

C# 6Compilel Platform “Roslyn”

Page 15: The Microsoft Compiler Platform (Roslyn). A quick overview

new C# 6 language features• Using static• Nameof operator• Property initializers• Constructor assignment to getter-only properties• Expression bodies• String interpolation• Null conditional• Index initializer• Exception filters• Await in catch/finally

Page 16: The Microsoft Compiler Platform (Roslyn). A quick overview

compiler API – syntax • Textual representation of source code• Represented as a tree data structure• Built from a sequence of tokens

Console . WriteLine ( x ) ;

identifier dot open paren close paren semicolonidentifier identifier

Page 17: The Microsoft Compiler Platform (Roslyn). A quick overview

compiler API – tokens• Identifiers• Console, Math, WriteLine, System, x, ...

• Keywords• if, static, class, return, ...

• Operators• +, *, &&, %, ...

• Punctuation• ., ;, ...

• Trivia• whitespace, comments

Page 18: The Microsoft Compiler Platform (Roslyn). A quick overview

compiler API – constructs

• Declarations• Types, Members

• Statements• if( ... )

• Clauses• else( ... ), where flag

• Expressions• Invocations or expressions using operators

Page 19: The Microsoft Compiler Platform (Roslyn). A quick overview

compiler API – trees

Console . WriteLine ( x ) ;

identifier dot open paren close parensemicolonidentifier identifier

WS CRLF

trivia trivia

SimpleMemberAccessExpression

IdentifierName

IdentifierName

InvocationExpression

ExpressionStatement

ArgumentList

Argument

IdentifierName

Page 20: The Microsoft Compiler Platform (Roslyn). A quick overview

compiler API – compilation• Code compilation as a service• Based upon• Syntax tree• Compilation options• Metadata references

• Use Emit to write to a file• Compilation results including diagnostics• Semantic analysis

Page 21: The Microsoft Compiler Platform (Roslyn). A quick overview

compiler API – symbols

• GetSemanticModel on a compilation• Get symbol information• Get speculative symbol information• Find the closest enclosing symbol from a

position• Check if symbols are accessible from a

position• Lookup symbols from a position• Getting constant values for literals

Page 22: The Microsoft Compiler Platform (Roslyn). A quick overview

compiler API – flow analysis• Data flow analysis• How does data flow into and out of a region• Useful for Extract Method analysis

• Control flow analysis• How does control get transferred in and out

of a region• Reachability• Entry points• Exit point• Return statements

Page 23: The Microsoft Compiler Platform (Roslyn). A quick overview

diagnostics and code fixes• Stylistic advice for source code• Like StyleCop rules• Implement your own rules• Supported in Visual Studio 2015

• Provide solutions to rule violations• Ligth bulb experience• Provides preview for code fixes

Page 24: The Microsoft Compiler Platform (Roslyn). A quick overview

analyzers and NuGet• Creating a VSIX extension for Visual

Studio to distribute analyzers• Create a NuGet package to install

analyzers inside your projects• Distribute analyzers with your NuGet

packages, for example to accommodate your frameworks and API’s

Page 25: The Microsoft Compiler Platform (Roslyn). A quick overview

scripting• Evaluate C# or VB expressions• Getting variables from scripts• Adding references and using namespaces• Dynamic support for scripts• Setting globals to pass data into a script• Creating a delegate from a script• REPL (read-eval-print-loop)

Page 26: The Microsoft Compiler Platform (Roslyn). A quick overview

and we’re done

Johnny Hooyberghs

[email protected]

@djohnnieke