the microsoft compiler platform (roslyn). a quick overview
Post on 09-Aug-2015
241 Views
Preview:
TRANSCRIPT
The Microsoft Compiler PlatformJohnny Hooyberghs
johnny.hooyberghs@involved-it.be
@djohnnieke
New events in the coming months
• May: no event• June
• Dennis Doomen (Netherlands)• Session TBA
• September• Christos Matskas (UK)• Session TBA
• October• You?
Thanks for partnering with Visug!
The Microsoft Compiler PlatformJohnny Hooyberghs
johnny.hooyberghs@involved-it.be
@djohnnieke
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
“
”
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
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
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
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
current compiler
compilerC#VB
IL
current compiler
parser symbols binder IL emitter
C#VB
IL
roslyn compiler
C#VB
ILSynta
x Tree API
Symbo
l API
Binding & Flow
Analysis API
Emit API
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
C# history
C# 1Managed
C# 2Generics
C# 3LINQ
C# 4Dynamic
programing
C# 5Asynchronous programing
C# 6Compilel Platform “Roslyn”
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
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
compiler API – tokens• Identifiers• Console, Math, WriteLine, System, x, ...
• Keywords• if, static, class, return, ...
• Operators• +, *, &&, %, ...
• Punctuation• ., ;, ...
• Trivia• whitespace, comments
compiler API – constructs
• Declarations• Types, Members
• Statements• if( ... )
• Clauses• else( ... ), where flag
• Expressions• Invocations or expressions using operators
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
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
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
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
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
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
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)
and we’re done
Johnny Hooyberghs
johnny.hooyberghs@involved-it.be
@djohnnieke
top related