| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Reflection.Internal; |
| | | 8 | | using System.Reflection.Metadata.Ecma335; |
| | | 9 | | |
| | | 10 | | namespace System.Reflection.Metadata |
| | | 11 | | { |
| | | 12 | | public readonly struct SequencePointCollection : IEnumerable<SequencePoint> |
| | | 13 | | { |
| | | 14 | | private readonly MemoryBlock _block; |
| | | 15 | | private readonly DocumentHandle _document; |
| | | 16 | | |
| | | 17 | | internal SequencePointCollection(MemoryBlock block, DocumentHandle document) |
| | 0 | 18 | | { |
| | 0 | 19 | | _block = block; |
| | 0 | 20 | | _document = document; |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | public Enumerator GetEnumerator() |
| | 0 | 24 | | { |
| | 0 | 25 | | return new Enumerator(_block, _document); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | IEnumerator IEnumerable.GetEnumerator() |
| | 0 | 29 | | { |
| | 0 | 30 | | return GetEnumerator(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | IEnumerator<SequencePoint> IEnumerable<SequencePoint>.GetEnumerator() |
| | 0 | 34 | | { |
| | 0 | 35 | | return GetEnumerator(); |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | public struct Enumerator : IEnumerator<SequencePoint> |
| | | 39 | | { |
| | | 40 | | private BlobReader _reader; |
| | | 41 | | private SequencePoint _current; |
| | | 42 | | private int _previousNonHiddenStartLine; |
| | | 43 | | private ushort _previousNonHiddenStartColumn; |
| | | 44 | | |
| | | 45 | | internal Enumerator(MemoryBlock block, DocumentHandle document) |
| | 0 | 46 | | { |
| | 0 | 47 | | _reader = new BlobReader(block); |
| | 0 | 48 | | _current = new SequencePoint(document, -1); |
| | 0 | 49 | | _previousNonHiddenStartLine = -1; |
| | 0 | 50 | | _previousNonHiddenStartColumn = 0; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | public bool MoveNext() |
| | 0 | 54 | | { |
| | 0 | 55 | | if (_reader.RemainingBytes == 0) |
| | 0 | 56 | | { |
| | 0 | 57 | | return false; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | DocumentHandle document = _current.Document; |
| | | 61 | | int offset, deltaLines, deltaColumns, startLine; |
| | | 62 | | ushort startColumn; |
| | | 63 | | |
| | 0 | 64 | | if (_reader.Offset == 0) |
| | 0 | 65 | | { |
| | | 66 | | // header (skip local signature rid): |
| | 0 | 67 | | _reader.ReadCompressedInteger(); |
| | | 68 | | |
| | 0 | 69 | | if (document.IsNil) |
| | 0 | 70 | | { |
| | 0 | 71 | | document = ReadDocumentHandle(); |
| | 0 | 72 | | } |
| | | 73 | | // IL offset: |
| | 0 | 74 | | offset = _reader.ReadCompressedInteger(); |
| | 0 | 75 | | } |
| | | 76 | | else |
| | 0 | 77 | | { |
| | | 78 | | // skip all document records and update the current document accordingly: |
| | | 79 | | int deltaOffset; |
| | 0 | 80 | | while ((deltaOffset = _reader.ReadCompressedInteger()) == 0) |
| | 0 | 81 | | { |
| | 0 | 82 | | document = ReadDocumentHandle(); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | // IL offset: |
| | 0 | 86 | | offset = AddOffsets(_current.Offset, deltaOffset); |
| | 0 | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | ReadDeltaLinesAndColumns(out deltaLines, out deltaColumns); |
| | | 90 | | |
| | | 91 | | // hidden |
| | 0 | 92 | | if (deltaLines == 0 && deltaColumns == 0) |
| | 0 | 93 | | { |
| | 0 | 94 | | _current = new SequencePoint(document, offset); |
| | 0 | 95 | | return true; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | // delta Start Line & Column: |
| | 0 | 99 | | if (_previousNonHiddenStartLine < 0) |
| | 0 | 100 | | { |
| | 0 | 101 | | Debug.Assert(_previousNonHiddenStartColumn == 0); |
| | | 102 | | |
| | 0 | 103 | | startLine = ReadLine(); |
| | 0 | 104 | | startColumn = ReadColumn(); |
| | 0 | 105 | | } |
| | | 106 | | else |
| | 0 | 107 | | { |
| | 0 | 108 | | startLine = AddLines(_previousNonHiddenStartLine, _reader.ReadCompressedSignedInteger()); |
| | 0 | 109 | | startColumn = AddColumns(_previousNonHiddenStartColumn, _reader.ReadCompressedSignedInteger()); |
| | 0 | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | _previousNonHiddenStartLine = startLine; |
| | 0 | 113 | | _previousNonHiddenStartColumn = startColumn; |
| | | 114 | | |
| | 0 | 115 | | _current = new SequencePoint( |
| | 0 | 116 | | document, |
| | 0 | 117 | | offset, |
| | 0 | 118 | | startLine, |
| | 0 | 119 | | startColumn, |
| | 0 | 120 | | AddLines(startLine, deltaLines), |
| | 0 | 121 | | AddColumns(startColumn, deltaColumns)); |
| | | 122 | | |
| | 0 | 123 | | return true; |
| | 0 | 124 | | } |
| | | 125 | | |
| | | 126 | | private void ReadDeltaLinesAndColumns(out int deltaLines, out int deltaColumns) |
| | 0 | 127 | | { |
| | 0 | 128 | | deltaLines = _reader.ReadCompressedInteger(); |
| | 0 | 129 | | deltaColumns = (deltaLines == 0) ? _reader.ReadCompressedInteger() : _reader.ReadCompressedSignedInteger |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | private int ReadLine() |
| | 0 | 133 | | { |
| | 0 | 134 | | return _reader.ReadCompressedInteger(); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | private ushort ReadColumn() |
| | 0 | 138 | | { |
| | 0 | 139 | | int column = _reader.ReadCompressedInteger(); |
| | 0 | 140 | | if (column > ushort.MaxValue) |
| | 0 | 141 | | { |
| | 0 | 142 | | Throw.SequencePointValueOutOfRange(); |
| | 0 | 143 | | } |
| | | 144 | | |
| | 0 | 145 | | return (ushort)column; |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | private static int AddOffsets(int value, int delta) |
| | 0 | 149 | | { |
| | 0 | 150 | | int result = unchecked(value + delta); |
| | 0 | 151 | | if (result < 0) |
| | 0 | 152 | | { |
| | 0 | 153 | | Throw.SequencePointValueOutOfRange(); |
| | 0 | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | return result; |
| | 0 | 157 | | } |
| | | 158 | | |
| | | 159 | | private static int AddLines(int value, int delta) |
| | 0 | 160 | | { |
| | 0 | 161 | | int result = unchecked(value + delta); |
| | 0 | 162 | | if (result < 0 || result >= SequencePoint.HiddenLine) |
| | 0 | 163 | | { |
| | 0 | 164 | | Throw.SequencePointValueOutOfRange(); |
| | 0 | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | return result; |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | private static ushort AddColumns(ushort value, int delta) |
| | 0 | 171 | | { |
| | 0 | 172 | | int result = unchecked(value + delta); |
| | 0 | 173 | | if (result < 0 || result >= ushort.MaxValue) |
| | 0 | 174 | | { |
| | 0 | 175 | | Throw.SequencePointValueOutOfRange(); |
| | 0 | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | return (ushort)result; |
| | 0 | 179 | | } |
| | | 180 | | |
| | | 181 | | private DocumentHandle ReadDocumentHandle() |
| | 0 | 182 | | { |
| | 0 | 183 | | int rowId = _reader.ReadCompressedInteger(); |
| | 0 | 184 | | if (rowId == 0 || !TokenTypeIds.IsValidRowId(rowId)) |
| | 0 | 185 | | { |
| | 0 | 186 | | Throw.InvalidHandle(); |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | return DocumentHandle.FromRowId(rowId); |
| | 0 | 190 | | } |
| | | 191 | | |
| | | 192 | | public SequencePoint Current |
| | | 193 | | { |
| | 0 | 194 | | get { return _current; } |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | object IEnumerator.Current |
| | | 198 | | { |
| | 0 | 199 | | get { return _current; } |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | public void Reset() |
| | 0 | 203 | | { |
| | 0 | 204 | | _reader.Reset(); |
| | 0 | 205 | | _current = default(SequencePoint); |
| | 0 | 206 | | } |
| | | 207 | | |
| | | 208 | | void IDisposable.Dispose() |
| | 0 | 209 | | { |
| | 0 | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |