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 '../dart2jslib.dart' show SourceSpan, MessageKind; | 7 import '../dart2jslib.dart' show SourceSpan, MessageKind; |
8 import '../elements/elements.dart' show AstElement; | 8 import '../elements/elements.dart' show |
| 9 AstElement, |
| 10 LocalElement; |
9 import '../scanner/scannerlib.dart' show Token; | 11 import '../scanner/scannerlib.dart' show Token; |
10 import '../tree/tree.dart' show Node; | 12 import '../tree/tree.dart' show Node; |
11 import '../js/js.dart' show JavaScriptNodeSourceInformation; | 13 import '../js/js.dart' show JavaScriptNodeSourceInformation; |
12 import 'source_file.dart'; | 14 import 'source_file.dart'; |
13 | 15 |
14 /// Interface for passing source information, for instance for use in source | 16 /// Interface for passing source information, for instance for use in source |
15 /// maps, through the backend. | 17 /// maps, through the backend. |
16 abstract class SourceInformation extends JavaScriptNodeSourceInformation { | 18 abstract class SourceInformation extends JavaScriptNodeSourceInformation { |
17 SourceSpan get sourceSpan; | 19 SourceSpan get sourceSpan; |
18 | 20 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 endPosition == other.endPosition; | 98 endPosition == other.endPosition; |
97 } | 99 } |
98 | 100 |
99 // TODO(johnniwinther): Remove this method. Source information should be | 101 // TODO(johnniwinther): Remove this method. Source information should be |
100 // computed based on the element by provided from statements and expressions. | 102 // computed based on the element by provided from statements and expressions. |
101 static StartEndSourceInformation computeSourceInformation( | 103 static StartEndSourceInformation computeSourceInformation( |
102 AstElement element) { | 104 AstElement element) { |
103 | 105 |
104 AstElement implementation = element.implementation; | 106 AstElement implementation = element.implementation; |
105 SourceFile sourceFile = implementation.compilationUnit.script.file; | 107 SourceFile sourceFile = implementation.compilationUnit.script.file; |
106 String name = element.name; | 108 String name = computeElementNameForSourceMaps(element); |
107 Node node = implementation.node; | 109 Node node = implementation.node; |
108 Token beginToken; | 110 Token beginToken; |
109 Token endToken; | 111 Token endToken; |
110 if (node == null) { | 112 if (node == null) { |
111 // Synthesized node. Use the enclosing element for the location. | 113 // Synthesized node. Use the enclosing element for the location. |
112 beginToken = endToken = element.position; | 114 beginToken = endToken = element.position; |
113 } else { | 115 } else { |
114 beginToken = node.getBeginToken(); | 116 beginToken = node.getBeginToken(); |
115 endToken = node.getEndToken(); | 117 endToken = node.getEndToken(); |
116 } | 118 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 } | 151 } |
150 } | 152 } |
151 | 153 |
152 /// [SourceInformationBuilder] that generates [PositionSourceInformation]. | 154 /// [SourceInformationBuilder] that generates [PositionSourceInformation]. |
153 class StartEndSourceInformationBuilder extends SourceInformationBuilder { | 155 class StartEndSourceInformationBuilder extends SourceInformationBuilder { |
154 final SourceFile sourceFile; | 156 final SourceFile sourceFile; |
155 final String name; | 157 final String name; |
156 | 158 |
157 StartEndSourceInformationBuilder(AstElement element) | 159 StartEndSourceInformationBuilder(AstElement element) |
158 : sourceFile = element.compilationUnit.script.file, | 160 : sourceFile = element.compilationUnit.script.file, |
159 name = element.name; | 161 name = computeElementNameForSourceMaps(element); |
160 | 162 |
161 SourceInformation buildDeclaration(AstElement element) { | 163 SourceInformation buildDeclaration(AstElement element) { |
162 return StartEndSourceInformation.computeSourceInformation(element); | 164 return StartEndSourceInformation.computeSourceInformation(element); |
163 } | 165 } |
164 | 166 |
165 SourceLocation sourceFileLocationForToken(Token token) { | 167 SourceLocation sourceFileLocationForToken(Token token) { |
166 SourceLocation location = | 168 SourceLocation location = |
167 new OffsetSourceLocation(sourceFile, token.charOffset, name); | 169 new OffsetSourceLocation(sourceFile, token.charOffset, name); |
168 checkValidSourceFileLocation(location, sourceFile, token.charOffset); | 170 checkValidSourceFileLocation(location, sourceFile, token.charOffset); |
169 return location; | 171 return location; |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 } | 334 } |
333 } | 335 } |
334 | 336 |
335 /// [SourceInformationBuilder] that generates [PositionSourceInformation]. | 337 /// [SourceInformationBuilder] that generates [PositionSourceInformation]. |
336 class PositionSourceInformationBuilder implements SourceInformationBuilder { | 338 class PositionSourceInformationBuilder implements SourceInformationBuilder { |
337 final SourceFile sourceFile; | 339 final SourceFile sourceFile; |
338 final String name; | 340 final String name; |
339 | 341 |
340 PositionSourceInformationBuilder(AstElement element) | 342 PositionSourceInformationBuilder(AstElement element) |
341 : sourceFile = element.implementation.compilationUnit.script.file, | 343 : sourceFile = element.implementation.compilationUnit.script.file, |
342 name = element.name; | 344 name = computeElementNameForSourceMaps(element); |
343 | 345 |
344 SourceInformation buildDeclaration(AstElement element) { | 346 SourceInformation buildDeclaration(AstElement element) { |
345 if (element.isSynthesized) { | 347 if (element.isSynthesized) { |
346 return new PositionSourceInformation( | 348 return new PositionSourceInformation( |
347 new OffsetSourceLocation( | 349 new OffsetSourceLocation( |
348 sourceFile, element.position.charOffset, name)); | 350 sourceFile, element.position.charOffset, name)); |
349 } else { | 351 } else { |
350 return new PositionSourceInformation( | 352 return new PositionSourceInformation( |
351 null, | 353 null, |
352 new OffsetSourceLocation(sourceFile, | 354 new OffsetSourceLocation(sourceFile, |
(...skipping 19 matching lines...) Expand all Loading... |
372 SourceInformation buildGet(Node node) => buildBegin(node); | 374 SourceInformation buildGet(Node node) => buildBegin(node); |
373 | 375 |
374 @override | 376 @override |
375 SourceInformation buildCall(Node node) => buildBegin(node); | 377 SourceInformation buildCall(Node node) => buildBegin(node); |
376 | 378 |
377 @override | 379 @override |
378 SourceInformationBuilder forContext(AstElement element) { | 380 SourceInformationBuilder forContext(AstElement element) { |
379 return new PositionSourceInformationBuilder(element); | 381 return new PositionSourceInformationBuilder(element); |
380 } | 382 } |
381 } | 383 } |
| 384 |
| 385 /// Compute the source map name for [element]. |
| 386 String computeElementNameForSourceMaps(AstElement element) { |
| 387 if (element.isClosure) { |
| 388 return computeElementNameForSourceMaps(element.enclosingElement); |
| 389 } else if (element.isClass) { |
| 390 return element.name; |
| 391 } else if (element.isConstructor || element.isGenerativeConstructorBody) { |
| 392 String className = element.enclosingClass.name; |
| 393 if (element.name == '') { |
| 394 return className; |
| 395 } |
| 396 return '$className.${element.name}'; |
| 397 } else if (element.isLocal) { |
| 398 LocalElement local = element; |
| 399 String name = local.name; |
| 400 if (name == '') { |
| 401 name = '<anonymous function>'; |
| 402 } |
| 403 return '${computeElementNameForSourceMaps(local.executableContext)}.$name'; |
| 404 } else if (element.enclosingClass != null) { |
| 405 if (element.enclosingClass.isClosure) { |
| 406 return computeElementNameForSourceMaps(element.enclosingClass); |
| 407 } |
| 408 return '${element.enclosingClass.name}.${element.name}'; |
| 409 } else { |
| 410 return element.name; |
| 411 } |
| 412 } |
OLD | NEW |