More Related Content Similar to Introduction to Roslyn (20) More from Yuki Igarashi (15) Introduction to Roslyn6. IS THE COMPILER A BLACK BOX?
• 既存のコンパイラはブラックボックス
基本的に出力は成果物(アセンブリ)のみ
途中で生成されたものはアセンブリ出力とともに消去
6
8. IS THE COMPILER A BLACK BOX?
8
リアルタイムエラー分析
Syntax Highlighting
And more …
IntelliSense(コード補間)
9. IS THE COMPILER A BLACK BOX?
• IDEはコードに関する深い知識が必要
• 知識を得るためには、それぞれがコード解析のため
のコードを書く必要がある
コンパイラ側と仕様を合わせる必要あり
そもそも解析器は本当に2つ以上必要?
9
18. .NETの仕様について
• コンパイラはネイティブコードではなくILを生成
18
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// コード サイズ 14 (0xe)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "Hello,Pronama!"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: nop
IL_000d: ret
} // end of method Pronama::Main
26. Compiler APIs
• コア部分
Microsoft.CodeAnalysis.dll
言語特有でない、すべての編集・解析プロセスを含む
Emit APIや、Syntax Tree APIの基本的・抽象的な構造、
及び(拡張)メソッドを持つ
26
27. Compiler APIs
• 言語別部分
Microsoft.CodeAnalysis.VisualBasic.dll
Microsoft.CodeAnalysis.CSharp.dll
コア部分を継承し、言語毎のパーサーや解析ロジックを持つ
実際にコードをコンパイルするときに呼び出される
27
28. Compiler APIs
• フロント部分
rcsc.exeやrvbc.exe (Roslyn版コンパイラ) 、そして
VBCSCompiler.exeを含む
ここではコマンドラインをパースして先ほどまでのレイ
ヤーを呼ぶだけ
28
30. Compiler APIs Demo1 (追記)
• 単純な構文のみを検証するプログラム
30
Dim tree = VisualBasicSyntaxTree.ParseText(code)
Dim errors As New List(Of ErrorItem)
For Each item In tree.GetDiagnostics
errors.Add(New ErrorItem With {.Message = item.GetMessage,
.Location = item.Location.SourceSpan.ToString,
.Id = item.Id,
.Severity = item.Severity.ToString})
Next
32. Compiler APIs Demo1 (追記)
• 実行結果に関して
単純に構文のエラーは解析できる
変数名の重複や存在しない名前(型やメソッド等)を入力し
ても通ってしまう
32
33. Compiler APIs Demo2 (追記)
• Semantic Analysisを用いて、意味も検証する
33
Dim tree = VisualBasicSyntaxTree.ParseText(code)
Dim compilation = VisualBasicCompilation.Create("pronama",
{tree},
{New MetadataFileReference(GetType(Object).Assembly.Location)})
Dim errors As New List(Of ErrorItem)
For Each item In compilation.GetDiagnostics
errors.Add(New ErrorItem With {.Message = item.GetMessage,
.Location = item.Location.SourceSpan.ToString,
.Id = item.Id,
.Severity = item.Severity.ToString})
Next
35. Compiler APIs Demo2 (追記)
• 実行結果に関して
構文エラーはもちろん解析可能
変数名の重複や存在しない名前(型やメソッド等)を入力し
たらエラーを出力してくれる
「変数が使われていない」等の警告も出力
データフロー解析(どこで変数を参照しているか等)も行え
る
35
36. Compiler APIs
• Scripting APIsに関して
( https://roslyn.codeplex.com/wikipage?title=FAQ#What happened to the REPL and
hosting scripting APIs より)
36
40. Workspace APIs
40
Dim workspace = MSBuild.MSBuildWorkspace.Create()
Dim solution = Await workspace.OpenSolutionAsync(dlg.FileName)
For Each projectId In solution.ProjectIds
Dim project = solution.GetProject(projectId)
Console.WriteLine("+ Project" + vbCrLf + " FileName: {0}, Lang: {1}, Id: {2}",
project.Name, project.Language, project.Id.ToString)
For Each docId In project.DocumentIds
Dim document = project.GetDocument(docId)
Console.WriteLine("- File" + vbCrLf + " Name: {0}, Path: {1}, ID: {2}",
document.Name, document.FilePath, document.Id)
Next
Next
• ソリューションに含まれるファイルの一覧を出力
41. Workspace APIs
41
Dim document = project.GetDocument(project.DocumentIds(0))
Dim version = Await document.GetTextVersionAsync()
Dim text = Await document.GetTextAsync()
Console.WriteLine("Version: {0}" + vbCrLf + “Text: {1}" version.ToString , text.ToString)
old = document
Dim newDocument = Await Formatter.FormatAsync(document)
Dim diff = Await newDocument.GetTextChangesAsync(old)
version = Await newDocument.GetTextVersionAsync()
text = Await newDocument.GetTextAsync()
Console.WriteLine("Version: {0}" + vbCrLf + “Text: {1}" version.ToString , text.ToString)
For Each change In diff
Console.WriteLine("*Change* + vbCrLf + "Place: {0}" + vbCrLf +"NewText: " ,
change.Span.ToString , change.NewText)
Next
• とあるファイルの整形を行い、その整形箇所を表示
43. Feature APIs
• 各種リファクタリングやQuick FixなどのAPIを提供
Microsoft.CodeAnalysis.Features.dll
NuGetでは Microsoft.CodeAnalysis.CodeActions という
名前
• 言語別で詳細なツールを作成することが可能
43
47. DLR vs. Roslyn
• Dynamic Language Runtime(動的言語ランタイム)
IronPythonやIronRubyをサポート
• DLRとRoslynは目的に違う
RoslynはVisual Studio他との関わり合いを重視
DLRは動的に動的言語に言語機能を提供するための技術
47
48. DLR vs. Roslyn
• ILの生成
DLRはSystem.Linq.Expressions 以下の式ツリーを使用
RoslynはEmit APIにより直接ILを生成している
=> パフォーマンスの向上につながる
48
49. DSL with Roslyn
• 既存のパーサーの構文の拡張も、オープンソースに
伴ってできるようになった(未確認)
• パーサーをすべて自前で用意してもよい
その場合自分のパーサーでRoslynのASTを生成すればよい
49
51. Roslynのこれまで
51
CTP
• 2011年10月
• Workspace APIとScripting API、また基本的なCompiler APIを提供
CTP2
• 2012年6月
• 匿名型やクエリ、イベント、lockステートメント他多数の言語サポート
CTP3
• 2012年9月
• LINQのサポート、遅延バインディング等
Preview
• 2014年4月
• async/awaitのサポート、次期バージョン機能の搭載
52. Roslynのロードマップ
• Review and refine the design of the REPL
window(scripting API).
• Discuss proposed language features.
• Remove the core compiler's dependency on the
full .NET framework allowing the use of the
Compilation data type on platforms like WinRT.
( http://roslyn.codeplex.com/wikipage?title=Roadmap&referringTitle=Documentation より抜粋)
52
54. Roslynが開く未来
• C#/VBへの新機能実装コストが減る
すでにRoslyn End User Previewではいくつかの新機能が
実装済み
どのような新機能が実装済み(予定)であるか、リストアッ
プされている
https://roslyn.codeplex.com/wikipage?title=Language%20Feat
ure%20Status&referringTitle=Documentation
54