| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library dart2js.source_information; | 5 library dart2js.source_information; |
| 6 | 6 |
| 7 import 'package:kernel/ast.dart' show Location; | 7 import 'package:kernel/ast.dart' show Location; |
| 8 import '../common.dart'; | 8 import '../common.dart'; |
| 9 import '../elements/elements.dart' | 9 import '../elements/elements.dart' |
| 10 show | 10 show |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 /// A location in a source file. | 156 /// A location in a source file. |
| 157 abstract class SourceLocation { | 157 abstract class SourceLocation { |
| 158 const SourceLocation(); | 158 const SourceLocation(); |
| 159 | 159 |
| 160 /// The absolute URI of the source file of this source location. | 160 /// The absolute URI of the source file of this source location. |
| 161 Uri get sourceUri; | 161 Uri get sourceUri; |
| 162 | 162 |
| 163 /// The character offset of the this source location into the source file. | 163 /// The character offset of the this source location into the source file. |
| 164 int get offset; | 164 int get offset; |
| 165 | 165 |
| 166 /// The 0-based line number of the [offset]. | 166 /// The 1-based line number of the [offset]. |
| 167 int get line; | 167 int get line; |
| 168 | 168 |
| 169 /// The 0-base column number of the [offset] with its line. | 169 /// The 1-based column number of the [offset] with its line. |
| 170 int get column; | 170 int get column; |
| 171 | 171 |
| 172 /// The name associated with this source location, if any. | 172 /// The name associated with this source location, if any. |
| 173 String get sourceName; | 173 String get sourceName; |
| 174 | 174 |
| 175 /// `true` if the offset within the length of the source file. | 175 /// `true` if the offset within the length of the source file. |
| 176 bool get isValid; | 176 bool get isValid; |
| 177 | 177 |
| 178 int get hashCode { | 178 int get hashCode { |
| 179 return sourceUri.hashCode * 17 + | 179 return sourceUri.hashCode * 17 + |
| 180 offset.hashCode * 17 + | 180 offset.hashCode * 17 + |
| 181 sourceName.hashCode * 23; | 181 sourceName.hashCode * 23; |
| 182 } | 182 } |
| 183 | 183 |
| 184 bool operator ==(other) { | 184 bool operator ==(other) { |
| 185 if (identical(this, other)) return true; | 185 if (identical(this, other)) return true; |
| 186 if (other is! SourceLocation) return false; | 186 if (other is! SourceLocation) return false; |
| 187 return sourceUri == other.sourceUri && | 187 return sourceUri == other.sourceUri && |
| 188 offset == other.offset && | 188 offset == other.offset && |
| 189 sourceName == other.sourceName; | 189 sourceName == other.sourceName; |
| 190 } | 190 } |
| 191 | 191 |
| 192 String get shortText { | 192 String get shortText => '${sourceUri.pathSegments.last}:[$line,$column]'; |
| 193 // Use 1-based line/column info to match usual dart tool output. | |
| 194 return '${sourceUri.pathSegments.last}:[${line + 1},${column + 1}]'; | |
| 195 } | |
| 196 | 193 |
| 197 String toString() { | 194 String toString() => '${sourceUri}:[${line},${column}]'; |
| 198 // Use 1-based line/column info to match usual dart tool output. | |
| 199 return '${sourceUri}:[${line + 1},${column + 1}]'; | |
| 200 } | |
| 201 } | 195 } |
| 202 | 196 |
| 203 /// A location in a source file. | 197 /// A location in a source file. |
| 204 abstract class AbstractSourceLocation extends SourceLocation { | 198 abstract class AbstractSourceLocation extends SourceLocation { |
| 205 final SourceFile _sourceFile; | 199 final SourceFile _sourceFile; |
| 206 Location _location; | 200 Location _location; |
| 207 | 201 |
| 208 AbstractSourceLocation(this._sourceFile) { | 202 AbstractSourceLocation(this._sourceFile) { |
| 209 assert(invariant(new SourceSpan(sourceUri, 0, 0), isValid, | 203 assert(invariant(new SourceSpan(sourceUri, 0, 0), isValid, |
| 210 message: "Invalid source location in ${sourceUri}: " | 204 message: "Invalid source location in ${sourceUri}: " |
| 211 "offset=$offset, length=${_sourceFile.length}.")); | 205 "offset=$offset, length=${_sourceFile.length}.")); |
| 212 } | 206 } |
| 213 | 207 |
| 214 /// The absolute URI of the source file of this source location. | 208 /// The absolute URI of the source file of this source location. |
| 215 Uri get sourceUri => _sourceFile.uri; | 209 Uri get sourceUri => _sourceFile.uri; |
| 216 | 210 |
| 217 /// The character offset of the this source location into the source file. | 211 /// The character offset of the this source location into the source file. |
| 218 int get offset; | 212 int get offset; |
| 219 | 213 |
| 220 /// The 0-based line number of the [offset]. | 214 /// The 1-based line number of the [offset]. |
| 221 int get line { | 215 int get line => (_location ??= _sourceFile.getLocation(offset)).line; |
| 222 _location ??= _sourceFile.getLocation(offset); | |
| 223 return _location.line - 1; | |
| 224 } | |
| 225 | 216 |
| 226 /// The 0-base column number of the [offset] with its line. | 217 /// The 1-based column number of the [offset] with its line. |
| 227 int get column { | 218 int get column => (_location ??= _sourceFile.getLocation(offset)).column; |
| 228 _location ??= _sourceFile.getLocation(offset); | |
| 229 return _location.column - 1; | |
| 230 } | |
| 231 | 219 |
| 232 /// The name associated with this source location, if any. | 220 /// The name associated with this source location, if any. |
| 233 String get sourceName; | 221 String get sourceName; |
| 234 | 222 |
| 235 /// `true` if the offset within the length of the source file. | 223 /// `true` if the offset within the length of the source file. |
| 236 bool get isValid => offset < _sourceFile.length; | 224 bool get isValid => offset < _sourceFile.length; |
| 237 | 225 |
| 238 String get shortText { | 226 String get shortText => '${sourceUri.pathSegments.last}:[$line,$column]'; |
| 239 // Use 1-based line/column info to match usual dart tool output. | |
| 240 return '${sourceUri.pathSegments.last}:[${line + 1},${column + 1}]'; | |
| 241 } | |
| 242 | 227 |
| 243 String toString() { | 228 String toString() => '${sourceUri}:[$line,$column]'; |
| 244 // Use 1-based line/column info to match usual dart tool output. | |
| 245 return '${sourceUri}:[${line + 1},${column + 1}]'; | |
| 246 } | |
| 247 } | 229 } |
| 248 | 230 |
| 249 class OffsetSourceLocation extends AbstractSourceLocation { | 231 class OffsetSourceLocation extends AbstractSourceLocation { |
| 250 final int offset; | 232 final int offset; |
| 251 final String sourceName; | 233 final String sourceName; |
| 252 | 234 |
| 253 OffsetSourceLocation(SourceFile sourceFile, this.offset, this.sourceName) | 235 OffsetSourceLocation(SourceFile sourceFile, this.offset, this.sourceName) |
| 254 : super(sourceFile); | 236 : super(sourceFile); |
| 255 | 237 |
| 256 String get shortText { | 238 String get shortText => '${super.shortText}:$sourceName'; |
| 257 return '${super.shortText}:$sourceName'; | |
| 258 } | |
| 259 | 239 |
| 260 String toString() { | 240 String toString() => '${super.toString()}:$sourceName'; |
| 261 return '${super.toString()}:$sourceName'; | |
| 262 } | |
| 263 } | 241 } |
| 264 | 242 |
| 265 /// Compute the source map name for [element]. | 243 /// Compute the source map name for [element]. |
| 266 String computeElementNameForSourceMaps(AstElement element) { | 244 String computeElementNameForSourceMaps(AstElement element) { |
| 267 if (element.isClosure) { | 245 if (element.isClosure) { |
| 268 return computeElementNameForSourceMaps(element.enclosingElement); | 246 return computeElementNameForSourceMaps(element.enclosingElement); |
| 269 } else if (element.isClass) { | 247 } else if (element.isClass) { |
| 270 return element.name; | 248 return element.name; |
| 271 } else if (element.isConstructor || element.isGenerativeConstructorBody) { | 249 } else if (element.isConstructor || element.isGenerativeConstructorBody) { |
| 272 String className = element.enclosingClass.name; | 250 String className = element.enclosingClass.name; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 @override | 316 @override |
| 339 int get line => null; | 317 int get line => null; |
| 340 | 318 |
| 341 @override | 319 @override |
| 342 int get offset => null; | 320 int get offset => null; |
| 343 | 321 |
| 344 String get shortName => '<no-location>'; | 322 String get shortName => '<no-location>'; |
| 345 | 323 |
| 346 String toString() => '<no-location>'; | 324 String toString() => '<no-location>'; |
| 347 } | 325 } |
| OLD | NEW |