|
| 1 | +/// Edit-and-Continue method debug information blobs. |
| 2 | +/// |
| 3 | +/// This module replicates, byte for byte, the three Portable-PDB CustomDebugInformation |
| 4 | +/// blob formats Roslyn persists per method to support Edit and Continue |
| 5 | +/// (roslyn/src/Compilers/Core/Portable/Emit/EditAndContinueMethodDebugInformation.cs): |
| 6 | +/// |
| 7 | +/// - EnC Local Slot Map (kind 755F52A8-91C5-45BE-B4B8-209571E552BD) |
| 8 | +/// - EnC Lambda and Closure Map (kind A643004C-0240-496F-A783-30D64F4979DE) |
| 9 | +/// - EnC State Machine State Map (kind 8B78CD68-2EDE-420B-980B-E15884B8AAA3) |
| 10 | +/// |
| 11 | +/// (GUIDs: roslyn/src/Dependencies/CodeAnalysis.Debugging/PortableCustomDebugInfoKinds.cs.) |
| 12 | +/// |
| 13 | +/// All multi-byte integers use the ECMA-335 compressed unsigned/signed encodings via |
| 14 | +/// System.Reflection.Metadata's BlobBuilder.WriteCompressedInteger / |
| 15 | +/// WriteCompressedSignedInteger and BlobReader.ReadCompressedInteger / |
| 16 | +/// ReadCompressedSignedInteger, exactly as Roslyn writes/reads them. |
| 17 | +/// |
| 18 | +/// Every "syntax offset" slot in these blobs is an opaque, caller-defined integer key |
| 19 | +/// (Roslyn: the syntax offset of the lambda/closure/state-machine-suspension syntax |
| 20 | +/// node). This module does not require the key to be a source offset; it only requires |
| 21 | +/// determinism across generations. tryEncodeOccurrenceKey/decodeOccurrenceKey provide one |
| 22 | +/// reusable way to pack a short (depth <= 2) ordinal chain into such a key. |
| 23 | +module internal FSharp.Compiler.AbstractIL.EncMethodDebugInformation |
| 24 | + |
| 25 | +/// Portable-PDB CustomDebugInformation kind GUIDs for the EnC blobs, copied verbatim |
| 26 | +/// from roslyn/src/Dependencies/CodeAnalysis.Debugging/PortableCustomDebugInfoKinds.cs. |
| 27 | +[<RequireQualifiedAccess>] |
| 28 | +module PortableCustomDebugInfoKinds = |
| 29 | + |
| 30 | + /// EnC Local Slot Map CDI kind. |
| 31 | + val encLocalSlotMap: System.Guid |
| 32 | + |
| 33 | + /// EnC Lambda and Closure Map CDI kind. |
| 34 | + val encLambdaAndClosureMap: System.Guid |
| 35 | + |
| 36 | + /// EnC State Machine State Map CDI kind. |
| 37 | + val encStateMachineStateMap: System.Guid |
| 38 | + |
| 39 | +/// Closure ordinal of a lambda that is lowered to a static (non-capturing) method. |
| 40 | +/// Mirrors Roslyn's LambdaDebugInfo.StaticClosureOrdinal. |
| 41 | +[<Literal>] |
| 42 | +val StaticClosureOrdinal: int = -1 |
| 43 | + |
| 44 | +/// Closure ordinal of a lambda closed over the 'this' pointer only. |
| 45 | +/// Mirrors Roslyn's LambdaDebugInfo.ThisOnlyClosureOrdinal. |
| 46 | +[<Literal>] |
| 47 | +val ThisOnlyClosureOrdinal: int = -2 |
| 48 | + |
| 49 | +/// Smallest valid closure ordinal. Mirrors Roslyn's LambdaDebugInfo.MinClosureOrdinal. |
| 50 | +[<Literal>] |
| 51 | +val MinClosureOrdinal: int = -2 |
| 52 | + |
| 53 | +/// Method ordinal of a method that has no lambda map (an empty blob decodes to this). |
| 54 | +/// Mirrors Roslyn's DebugId.UndefinedOrdinal. |
| 55 | +[<Literal>] |
| 56 | +val UndefinedMethodOrdinal: int = -1 |
| 57 | + |
| 58 | +/// Largest synthesized-local kind serializable in the slot map: the kind is stored as |
| 59 | +/// (kind + 1) in bits 0-5 of the leading byte (bit 6 is unused, bit 7 flags a trailing ordinal), and |
| 60 | +/// Roslyn's reader recovers it with mask 0x3F, so only kinds 0..0x3E round-trip. |
| 61 | +[<Literal>] |
| 62 | +val MaxSerializableLocalKind: int = 0x3E |
| 63 | + |
| 64 | +/// One slot in the EnC Local Slot Map: the local variable layout of a method body, |
| 65 | +/// recorded so a later generation can map its locals onto the same slot indices. |
| 66 | +[<RequireQualifiedAccess>] |
| 67 | +type EncLocalSlotInfo = |
| 68 | + /// A short-lived lowering temp: serialized as the single byte 0x00, carrying no |
| 69 | + /// identity (a later generation never reuses it). |
| 70 | + | Temp |
| 71 | + |
| 72 | + /// A long-lived synthesized local. |
| 73 | + /// kind: synthesized-local kind (Roslyn SynthesizedLocalKind value, 0..MaxSerializableLocalKind; |
| 74 | + /// 0 = user-defined local). |
| 75 | + /// syntaxOffset: caller-defined key of the declaring occurrence |
| 76 | + /// (Roslyn: syntax offset of the local's declarator). |
| 77 | + /// ordinal: zero-based disambiguator among slots sharing the same kind and offset (>= 0). |
| 78 | + | Slot of kind: int * syntaxOffset: int * ordinal: int |
| 79 | + |
| 80 | +/// One closure scope in the EnC Lambda and Closure Map. The closure's ordinal is its |
| 81 | +/// index in EncMethodDebugInformation.Closures; lambdas reference closures by that index. |
| 82 | +type EncClosureInfo = |
| 83 | + { |
| 84 | + /// Caller-defined key (Roslyn: syntax offset of the scope owning the closure). |
| 85 | + SyntaxOffset: int |
| 86 | + } |
| 87 | + |
| 88 | +/// One lambda in the EnC Lambda and Closure Map. |
| 89 | +type EncLambdaInfo = |
| 90 | + { |
| 91 | + /// Caller-defined key (Roslyn: syntax offset of the lambda body). |
| 92 | + SyntaxOffset: int |
| 93 | + /// Index into EncMethodDebugInformation.Closures of the closure holding the |
| 94 | + /// lambda's captures, or StaticClosureOrdinal / ThisOnlyClosureOrdinal. |
| 95 | + ClosureOrdinal: int |
| 96 | + } |
| 97 | + |
| 98 | +/// One suspension point in the EnC State Machine State Map. |
| 99 | +type EncStateMachineStateInfo = |
| 100 | + { |
| 101 | + /// State machine state number assigned to the suspension point (may be negative: |
| 102 | + /// Roslyn uses negative numbers for increasing-iteration finalize states). |
| 103 | + StateNumber: int |
| 104 | + /// Caller-defined key (Roslyn: syntax offset of the await/yield syntax node). |
| 105 | + SyntaxOffset: int |
| 106 | + } |
| 107 | + |
| 108 | +/// Debugging information associated with a method, persisted by the compiler in the |
| 109 | +/// Portable PDB to support Edit and Continue. Mirrors Roslyn's |
| 110 | +/// EditAndContinueMethodDebugInformation. |
| 111 | +type EncMethodDebugInformation = |
| 112 | + { |
| 113 | + /// Ordinal of the method within its generation (>= -1; UndefinedMethodOrdinal when absent). |
| 114 | + MethodOrdinal: int |
| 115 | + /// Local slot layout, in slot-index order (EnC Local Slot Map). |
| 116 | + LocalSlots: EncLocalSlotInfo list |
| 117 | + /// Closure scopes, in ordinal order (EnC Lambda and Closure Map). |
| 118 | + Closures: EncClosureInfo list |
| 119 | + /// Lambdas, in ordinal order (EnC Lambda and Closure Map). |
| 120 | + Lambdas: EncLambdaInfo list |
| 121 | + /// State machine suspension points (EnC State Machine State Map). |
| 122 | + StateMachineStates: EncStateMachineStateInfo list |
| 123 | + } |
| 124 | + |
| 125 | + /// An empty map (no slots, lambdas, closures or states; undefined method ordinal). |
| 126 | + static member Empty: EncMethodDebugInformation |
| 127 | + |
| 128 | +/// Packs an ordinal chain (root-first enclosing ordinals, ending with the innermost |
| 129 | +/// ordinal) into a deterministic int suitable for a "syntax offset" blob slot. Fails |
| 130 | +/// closed (None) past the limits: chains deeper than 2, ordinals > 0xFFFF, or keys |
| 131 | +/// exceeding the compressed-integer budget. |
| 132 | +val tryEncodeOccurrenceKey: ordinalChain: int list -> int option |
| 133 | + |
| 134 | +/// Unpacks an occurrence key produced by tryEncodeOccurrenceKey back into its |
| 135 | +/// root-first ordinal chain. |
| 136 | +val decodeOccurrenceKey: key: int -> int list |
| 137 | + |
| 138 | +/// Serializes the EnC Local Slot Map blob for 'info', byte-for-byte as Roslyn's |
| 139 | +/// SerializeLocalSlots. Returns the empty array when there are no slots (no CDI row |
| 140 | +/// should be emitted then). |
| 141 | +val serializeLocalSlots: info: EncMethodDebugInformation -> byte[] |
| 142 | + |
| 143 | +/// Deserializes an EnC Local Slot Map blob, byte-for-byte as Roslyn's UncompressSlotMap. |
| 144 | +/// An empty (or null) blob yields no slots. |
| 145 | +val deserializeLocalSlots: blob: byte[] -> EncLocalSlotInfo list |
| 146 | + |
| 147 | +/// Serializes the EnC Lambda and Closure Map blob for 'info', byte-for-byte as Roslyn's |
| 148 | +/// SerializeLambdaMap. Returns the empty array when there are no lambdas and no closures |
| 149 | +/// (Roslyn's MetadataWriter skips the CDI row in that case; note the method ordinal is |
| 150 | +/// then not persisted and decodes back as UndefinedMethodOrdinal). |
| 151 | +val serializeLambdaMap: info: EncMethodDebugInformation -> byte[] |
| 152 | + |
| 153 | +/// Deserializes an EnC Lambda and Closure Map blob, byte-for-byte as Roslyn's |
| 154 | +/// UncompressLambdaMap. An empty (or null) blob yields (UndefinedMethodOrdinal, [], []). |
| 155 | +val deserializeLambdaMap: blob: byte[] -> int * EncClosureInfo list * EncLambdaInfo list |
| 156 | + |
| 157 | +/// Serializes the EnC State Machine State Map blob for 'info', byte-for-byte as |
| 158 | +/// Roslyn's SerializeStateMachineStates: entries are sorted by syntax offset (stably, |
| 159 | +/// preserving relative order of equal offsets, which encodes the per-offset relative |
| 160 | +/// ordinal). Returns the empty array when there are no states (no CDI row then). |
| 161 | +val serializeStateMachineStates: info: EncMethodDebugInformation -> byte[] |
| 162 | + |
| 163 | +/// Deserializes an EnC State Machine State Map blob, byte-for-byte as Roslyn's |
| 164 | +/// UncompressStateMachineStates (including the ordered-by-offset and <= 256-per-offset |
| 165 | +/// validations). An empty (or null) blob yields no states. |
| 166 | +val deserializeStateMachineStates: blob: byte[] -> EncStateMachineStateInfo list |
| 167 | + |
| 168 | +/// Deserializes EnC method debug information from the three blobs (any of which may be |
| 169 | +/// null or empty). Mirrors Roslyn's EditAndContinueMethodDebugInformation.Create. |
| 170 | +val deserialize: |
| 171 | + slotMapBlob: byte[] -> lambdaMapBlob: byte[] -> stateMachineStateMapBlob: byte[] -> EncMethodDebugInformation |
| 172 | + |
| 173 | +/// Decodes every method-level EnC CustomDebugInformation row of a portable PDB image into |
| 174 | +/// per-method EnC debug information, keyed by MethodDef token (0x06xxxxxx). The CDI parent |
| 175 | +/// of the EnC rows is always a MethodDef handle, so token keying is unambiguous. |
| 176 | +/// Fail safe: a null/empty or non-PDB image yields the empty map, and a method whose |
| 177 | +/// blobs do not decode is omitted rather than guessed. |
| 178 | +val readEncMethodDebugInfoFromPortablePdb: pdbBytes: byte[] -> Map<int, EncMethodDebugInformation> |
0 commit comments