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 '../common.dart'; | 7 import '../common.dart'; |
8 import '../elements/elements.dart' show AstElement, LocalElement, ResolvedAst; | 8 import '../elements/elements.dart' |
| 9 show |
| 10 AstElement, |
| 11 CompilationUnitElement, |
| 12 LocalElement, |
| 13 ResolvedAst, |
| 14 ResolvedAstKind; |
9 import '../js/js.dart' show JavaScriptNodeSourceInformation; | 15 import '../js/js.dart' show JavaScriptNodeSourceInformation; |
| 16 import '../script.dart'; |
10 import '../tree/tree.dart' show Node; | 17 import '../tree/tree.dart' show Node; |
11 import 'source_file.dart'; | 18 import 'source_file.dart'; |
12 | 19 |
13 /// Interface for passing source information, for instance for use in source | 20 /// Interface for passing source information, for instance for use in source |
14 /// maps, through the backend. | 21 /// maps, through the backend. |
15 abstract class SourceInformation extends JavaScriptNodeSourceInformation { | 22 abstract class SourceInformation extends JavaScriptNodeSourceInformation { |
16 const SourceInformation(); | 23 const SourceInformation(); |
17 | 24 |
18 SourceSpan get sourceSpan; | 25 SourceSpan get sourceSpan; |
19 | 26 |
(...skipping 10 matching lines...) Expand all Loading... |
30 List<SourceLocation> get sourceLocations; | 37 List<SourceLocation> get sourceLocations; |
31 | 38 |
32 /// Return a short textual representation of the source location. | 39 /// Return a short textual representation of the source location. |
33 String get shortText; | 40 String get shortText; |
34 } | 41 } |
35 | 42 |
36 /// Strategy for creating, processing and applying [SourceInformation]. | 43 /// Strategy for creating, processing and applying [SourceInformation]. |
37 class SourceInformationStrategy { | 44 class SourceInformationStrategy { |
38 const SourceInformationStrategy(); | 45 const SourceInformationStrategy(); |
39 | 46 |
40 /// Create a [SourceInformationBuilder] for [element]. | 47 /// Create a [SourceInformationBuilder] for [resolvedAst]. |
41 SourceInformationBuilder createBuilderForContext(AstElement element) { | 48 SourceInformationBuilder createBuilderForContext(ResolvedAst resolvedAst) { |
42 return const SourceInformationBuilder(); | 49 return const SourceInformationBuilder(); |
43 } | 50 } |
44 | 51 |
45 /// Generate [SourceInformation] marker for non-preamble code. | 52 /// Generate [SourceInformation] marker for non-preamble code. |
46 SourceInformation buildSourceMappedMarker() => null; | 53 SourceInformation buildSourceMappedMarker() => null; |
47 | 54 |
48 /// Called when compilation has completed. | 55 /// Called when compilation has completed. |
49 void onComplete() {} | 56 void onComplete() {} |
50 } | 57 } |
51 | 58 |
52 /// Interface for generating [SourceInformation]. | 59 /// Interface for generating [SourceInformation]. |
53 class SourceInformationBuilder { | 60 class SourceInformationBuilder { |
54 const SourceInformationBuilder(); | 61 const SourceInformationBuilder(); |
55 | 62 |
56 /// Create a [SourceInformationBuilder] for [element]. | 63 /// Create a [SourceInformationBuilder] for [resolvedAst]. |
57 SourceInformationBuilder forContext(AstElement element) => this; | 64 SourceInformationBuilder forContext(ResolvedAst resolvedAst) => this; |
58 | 65 |
59 /// Generate [SourceInformation] the declaration of the element in | 66 /// Generate [SourceInformation] the declaration of the element in |
60 /// [resolvedAst]. | 67 /// [resolvedAst]. |
61 SourceInformation buildDeclaration(ResolvedAst resolvedAst) => null; | 68 SourceInformation buildDeclaration(ResolvedAst resolvedAst) => null; |
62 | 69 |
63 /// Generate [SourceInformation] for the generic [node]. | 70 /// Generate [SourceInformation] for the generic [node]. |
64 @deprecated | 71 @deprecated |
65 SourceInformation buildGeneric(Node node) => null; | 72 SourceInformation buildGeneric(Node node) => null; |
66 | 73 |
67 /// Generate [SourceInformation] for an instantiation of a class using [node] | 74 /// Generate [SourceInformation] for an instantiation of a class using [node] |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 /// Generate [SourceInformation] for the switch case in [node]. | 151 /// Generate [SourceInformation] for the switch case in [node]. |
145 SourceInformation buildSwitchCase(Node node) => null; | 152 SourceInformation buildSwitchCase(Node node) => null; |
146 } | 153 } |
147 | 154 |
148 /// A location in a source file. | 155 /// A location in a source file. |
149 abstract class SourceLocation { | 156 abstract class SourceLocation { |
150 final SourceFile _sourceFile; | 157 final SourceFile _sourceFile; |
151 int _line; | 158 int _line; |
152 | 159 |
153 SourceLocation(this._sourceFile) { | 160 SourceLocation(this._sourceFile) { |
154 assert(isValid); | 161 assert(invariant(new SourceSpan(sourceUri, 0, 0), isValid, |
| 162 message: "Invalid source location in ${sourceUri}: " |
| 163 "offset=$offset, length=${_sourceFile.length}.")); |
155 } | 164 } |
156 | 165 |
157 /// The absolute URI of the source file of this source location. | 166 /// The absolute URI of the source file of this source location. |
158 Uri get sourceUri => _sourceFile.uri; | 167 Uri get sourceUri => _sourceFile.uri; |
159 | 168 |
160 /// The character offset of the this source location into the source file. | 169 /// The character offset of the this source location into the source file. |
161 int get offset; | 170 int get offset; |
162 | 171 |
163 /// The 0-based line number of the [offset]. | 172 /// The 0-based line number of the [offset]. |
164 int get line { | 173 int get line { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 return '${computeElementNameForSourceMaps(local.executableContext)}.$name'; | 246 return '${computeElementNameForSourceMaps(local.executableContext)}.$name'; |
238 } else if (element.enclosingClass != null) { | 247 } else if (element.enclosingClass != null) { |
239 if (element.enclosingClass.isClosure) { | 248 if (element.enclosingClass.isClosure) { |
240 return computeElementNameForSourceMaps(element.enclosingClass); | 249 return computeElementNameForSourceMaps(element.enclosingClass); |
241 } | 250 } |
242 return '${element.enclosingClass.name}.${element.name}'; | 251 return '${element.enclosingClass.name}.${element.name}'; |
243 } else { | 252 } else { |
244 return element.name; | 253 return element.name; |
245 } | 254 } |
246 } | 255 } |
| 256 |
| 257 /// Computes the [SourceFile] for the source code of [resolvedAst]. |
| 258 SourceFile computeSourceFile(ResolvedAst resolvedAst) { |
| 259 SourceFile sourceFile; |
| 260 if (resolvedAst.kind != ResolvedAstKind.PARSED) { |
| 261 // Synthesized node. Use the enclosing element for the location. |
| 262 sourceFile = resolvedAst.element.compilationUnit.script.file; |
| 263 } else { |
| 264 Uri uri = resolvedAst.sourceUri; |
| 265 AstElement implementation = resolvedAst.element.implementation; |
| 266 Script script = implementation.compilationUnit.script; |
| 267 if (uri == script.resourceUri) { |
| 268 sourceFile = script.file; |
| 269 } else { |
| 270 // Slow path, happens only for deserialized elements. |
| 271 // TODO(johnniwinther): Support a way to get a [SourceFile] from a |
| 272 // [Uri]. |
| 273 for (CompilationUnitElement compilationUnit |
| 274 in implementation.library.compilationUnits) { |
| 275 Script script = compilationUnit.script; |
| 276 if (uri == script.resourceUri) { |
| 277 sourceFile = script.file; |
| 278 break; |
| 279 } |
| 280 } |
| 281 } |
| 282 } |
| 283 return sourceFile; |
| 284 } |
OLD | NEW |