# アーキテクチャ構成図

## システム全体構成図

```mermaid
flowchart TB
    subgraph Clients["クライアント"]
        CLI["コマンドライン<br/>dotnet build / fsc"]
        VS["Visual Studio"]
        VSCode["VS Code"]
        REPL["F# Interactive<br/>dotnet fsi"]
    end

    subgraph Tools["ツール層"]
        FSC["fsc<br/>コンパイラ実行ファイル"]
        FSI["fsi<br/>F# Interactive実行ファイル"]
        LSP["FSharp.Compiler.LanguageServer"]
        Editor["FSharp.Editor<br/>VS拡張"]
    end

    subgraph Core["コア層"]
        FCS["FSharp.Compiler.Service"]
        FSharpCore["FSharp.Core"]
        FSharpBuild["FSharp.Build<br/>MSBuildタスク"]
    end

    subgraph External["外部依存"]
        SDK[".NET SDK"]
        MSBuild["MSBuild"]
        NuGet["NuGet"]
    end

    CLI --> FSC
    CLI --> FSharpBuild
    VS --> Editor
    VSCode --> LSP
    REPL --> FSI

    FSC --> FCS
    FSI --> FCS
    LSP --> FCS
    Editor --> FCS

    FCS --> FSharpCore
    FSharpBuild --> MSBuild
    FSI --> NuGet

    FCS --> SDK
    FSharpCore --> SDK
```

## コンパイラパイプライン構成図

```mermaid
flowchart LR
    subgraph Input["入力"]
        Source["ソースファイル<br/>.fs / .fsi"]
        Refs["参照アセンブリ<br/>.dll"]
        Args["コマンドライン引数"]
    end

    subgraph Lexing["字句解析"]
        Tokenizer["トークナイザ<br/>lex.fsl"]
        LexFilter["空白文字感知変換<br/>LexFilter.fs"]
    end

    subgraph Parsing["構文解析"]
        Parser["パーサ<br/>pars.fsy"]
        AST["AST<br/>SyntaxTree.fs"]
    end

    subgraph Import["参照読込"]
        ILRead["ILリーダー<br/>ilread.fs"]
        Pickle["F#メタデータ<br/>TypedTreePickle.fs"]
    end

    subgraph TypeCheck["型検査"]
        Check["型検査<br/>CheckDeclarations.fs"]
        Solver["制約解決<br/>ConstraintSolver.fs"]
        NameRes["名前解決<br/>NameResolution.fs"]
        TypedTree["TypedTree"]
    end

    subgraph Optimize["最適化"]
        Opt["最適化<br/>Optimizer.fs"]
        Lower["Lowering<br/>LowerStateMachines.fs"]
        Lambda["Lambda Lifting<br/>InnerLambdasToTopLevelFuncs.fs"]
    end

    subgraph CodeGen["コード生成"]
        IlxGen["IL生成<br/>IlxGen.fs"]
        Erase["クロージャ消去<br/>EraseClosures.fs"]
        AbstractIL["Abstract IL"]
    end

    subgraph Emit["出力"]
        ILWrite["ILライター<br/>ilwrite.fs"]
        Assembly["アセンブリ<br/>.dll / .exe"]
        PDB["PDB"]
    end

    Source --> Tokenizer
    Tokenizer --> LexFilter
    LexFilter --> Parser
    Parser --> AST

    Refs --> ILRead
    ILRead --> Pickle
    Pickle --> Import

    AST --> Check
    Import --> Check
    Check --> Solver
    Solver --> NameRes
    NameRes --> TypedTree

    TypedTree --> Opt
    Opt --> Lower
    Lower --> Lambda
    Lambda --> IlxGen

    IlxGen --> Erase
    Erase --> AbstractIL
    AbstractIL --> ILWrite
    ILWrite --> Assembly
    ILWrite --> PDB
```

## レイヤー構成図

```mermaid
flowchart TB
    subgraph Service["言語サービス層"]
        ServiceAPI["Service API<br/>service.fs"]
        BackgroundCompiler["BackgroundCompiler.fs"]
        IncrementalBuild["IncrementalBuild.fs"]
        TransparentCompiler["TransparentCompiler.fs"]
    end

    subgraph Symbols["シンボル層"]
        SymbolAPI["Symbols.fs"]
        FSharpDiagnostic["FSharpDiagnostic.fs"]
        Exprs["Exprs.fs"]
    end

    subgraph Driver["ドライバー層"]
        FSCDriver["fsc.fs"]
        CompilerConfig["CompilerConfig.fs"]
        ParseAndCheck["ParseAndCheckInputs.fs"]
        CompilerOptions["CompilerOptions.fs"]
    end

    subgraph CodeGenLayer["コード生成層"]
        IlxGenModule["IlxGen.fs"]
        EraseClosures["EraseClosures.fs"]
        EraseUnions["EraseUnions.fs"]
    end

    subgraph OptimizeLayer["最適化層"]
        Optimizer["Optimizer.fs"]
        DetupleArgs["DetupleArgs.fs"]
        LowerSeq["LowerSequences.fs"]
        LowerState["LowerStateMachines.fs"]
    end

    subgraph CheckingLayer["型検査層"]
        CheckDecl["CheckDeclarations.fs"]
        CheckExpr["CheckExpressions.fs"]
        ConstraintSolverMod["ConstraintSolver.fs"]
        NameResMod["NameResolution.fs"]
        PatternMatch["PatternMatchCompilation.fs"]
    end

    subgraph TypedTreeLayer["型付き構文木層"]
        TypedTreeMod["TypedTree.fs"]
        TypedTreeOps["TypedTreeOps.fs"]
        TcGlobals["TcGlobals.fs"]
        TypeProviders["TypeProviders.fs"]
    end

    subgraph SyntaxTreeLayer["構文木層"]
        SyntaxTreeMod["SyntaxTree.fs"]
        SyntaxTreeOps["SyntaxTreeOps.fs"]
        LexFilterMod["LexFilter.fs"]
        ParseHelpersMod["ParseHelpers.fs"]
    end

    subgraph AbstractILLayer["Abstract IL層"]
        ILMod["il.fs"]
        ILRead["ilread.fs"]
        ILWrite["ilwrite.fs"]
        ILReflect["ilreflect.fs"]
    end

    subgraph FacilitiesLayer["基盤機能層"]
        DiagLogger["DiagnosticsLogger.fs"]
        LangFeatures["LanguageFeatures.fs"]
        PrimLex["prim-lexing.fs"]
        PrimParse["prim-parsing.fs"]
    end

    subgraph UtilitiesLayer["ユーティリティ層"]
        ILLib["illib.fs"]
        FileSystem["FileSystem.fs"]
        Caches["Caches.fs"]
        Collections["TaggedCollections.fs"]
    end

    Service --> Symbols
    Service --> Driver
    Symbols --> CheckingLayer
    Driver --> CodeGenLayer
    Driver --> OptimizeLayer
    CodeGenLayer --> OptimizeLayer
    OptimizeLayer --> CheckingLayer
    CheckingLayer --> TypedTreeLayer
    TypedTreeLayer --> SyntaxTreeLayer
    CodeGenLayer --> AbstractILLayer
    SyntaxTreeLayer --> FacilitiesLayer
    AbstractILLayer --> FacilitiesLayer
    FacilitiesLayer --> UtilitiesLayer
```

## コンパイルフェーズ状態遷移図

```mermaid
stateDiagram-v2
    [*] --> Inputs: ソース/参照ファイル

    state "コンパイルフェーズ" as Compilation {
        Inputs --> Lexing

        state Lexing {
            [*] --> BasicLexing
            BasicLexing --> WhitespaceLexing: トークンストリーム
            WhitespaceLexing --> [*]: F#オフサイドルール適用済み
        }

        Lexing --> Parsing

        state Parsing {
            [*] --> ParseTokens
            ParseTokens --> [*]: AST生成
        }

        state Import {
            [*] --> ResolveRefs
            ResolveRefs --> ImportDotNet
            ResolveRefs --> ImportFSharp
            ImportDotNet --> [*]
            ImportFSharp --> [*]
        }

        Parsing --> TypeChecking
        Import --> TypeChecking

        state TypeChecking {
            [*] --> SequentialCheck
            SequentialCheck --> PatternCompile
            PatternCompile --> ConstraintSolve
            ConstraintSolve --> PostCheck
            PostCheck --> [*]
        }

        TypeChecking --> CodeGeneration

        state CodeGeneration {
            [*] --> QuotationTrans
            QuotationTrans --> Optimization
            Optimization --> ILGen
            ILGen --> ILRewrite
            ILRewrite --> [*]
        }

        CodeGeneration --> ILEmit

        state ILEmit {
            [*] --> BinaryEmit
            [*] --> ReflectionEmit
            BinaryEmit --> Output
            ReflectionEmit --> Output
        }
    }

    ILEmit --> [*]: アセンブリ出力
```

## 言語サービスアーキテクチャ

```mermaid
flowchart TB
    subgraph Client["クライアント"]
        IDE["IDE / エディタ"]
    end

    subgraph LSP["Language Server Protocol"]
        Server["FSharpLanguageServer"]
        Handlers["リクエストハンドラ"]
    end

    subgraph Checker["FSharpChecker"]
        Background["BackgroundCompiler"]
        Transparent["TransparentCompiler"]
        Incremental["IncrementalBuild"]
    end

    subgraph Cache["キャッシュ層"]
        AsyncMemo["AsyncMemoize"]
        LRU["LruCache"]
        ItemCache["ItemKeyStore"]
    end

    subgraph Analysis["解析結果"]
        ParseResults["FSharpParseFileResults"]
        CheckResults["FSharpCheckFileResults"]
        ProjectResults["FSharpCheckProjectResults"]
    end

    IDE <-->|LSP| Server
    Server --> Handlers
    Handlers --> Background
    Handlers --> Transparent

    Background --> Incremental
    Transparent --> Incremental

    Incremental --> AsyncMemo
    AsyncMemo --> LRU

    Incremental --> ParseResults
    Incremental --> CheckResults
    Background --> ProjectResults
```

## パッケージ依存関係図

```mermaid
flowchart TB
    subgraph Executables["実行ファイル"]
        fsc["fsc"]
        fsi["fsi"]
    end

    subgraph Libraries["ライブラリ"]
        FCS["FSharp.Compiler.Service"]
        Core["FSharp.Core"]
        Build["FSharp.Build"]
        DepMgr["FSharp.DependencyManager.Nuget"]
        LSP["FSharp.Compiler.LanguageServer"]
    end

    subgraph VSIntegration["VS統合"]
        Editor["FSharp.Editor"]
        LangService["FSharp.LanguageService"]
        ProjectSystem["FSharp.ProjectSystem"]
    end

    subgraph External["外部依存"]
        SysReflection["System.Reflection.Metadata"]
        SysCollections["System.Collections.Immutable"]
        SysMemory["System.Memory"]
        MSBuildFW["Microsoft.Build.Framework"]
    end

    fsc --> FCS
    fsi --> FCS
    LSP --> FCS

    FCS --> Core
    FCS --> DepMgr
    FCS --> SysReflection
    FCS --> SysCollections
    FCS --> SysMemory

    Build --> MSBuildFW

    Editor --> FCS
    Editor --> LangService
    LangService --> FCS
    ProjectSystem --> FCS
```

## ビルドシステム構成図

```mermaid
flowchart LR
    subgraph BuildTools["ビルドツール"]
        FsLex["fslex"]
        FsYacc["fsyacc"]
        AssemblyCheck["AssemblyCheck"]
    end

    subgraph Sources["ソース生成"]
        LexFsl["lex.fsl"]
        ParsFsy["pars.fsy"]
        PPLexFsl["pplex.fsl"]
        PPParsFsy["pppars.fsy"]
    end

    subgraph Generated["生成ファイル"]
        LexFs["lex.fs"]
        ParsFs["pars.fs"]
        PPLexFs["pplex.fs"]
        PPParsFs["pppars.fs"]
    end

    subgraph Bootstrap["ブートストラップ"]
        Proto["Protoコンパイラ"]
        Final["Finalコンパイラ"]
    end

    LexFsl --> FsLex --> LexFs
    ParsFsy --> FsYacc --> ParsFs
    PPLexFsl --> FsLex --> PPLexFs
    PPParsFsy --> FsYacc --> PPParsFs

    Proto -->|ビルド| Final
    Final -->|セルフコンパイル| Final
```
