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 /// Source information system that maps spans of Dart AST nodes to spans of | 5 /// Source information system that maps spans of Dart AST nodes to spans of |
6 /// JavaScript nodes. | 6 /// JavaScript nodes. |
7 | 7 |
8 library dart2js.source_information.start_end; | 8 library dart2js.source_information.start_end; |
9 | 9 |
10 import '../common.dart'; | 10 import '../common.dart'; |
11 import '../diagnostics/messages.dart' show MessageTemplate; | 11 import '../diagnostics/messages.dart' show MessageTemplate; |
12 import '../elements/elements.dart' show AstElement; | 12 import '../elements/elements.dart' show AstElement, ResolvedAst, ResolvedAstKind
; |
13 import '../js/js.dart' as js; | 13 import '../js/js.dart' as js; |
14 import '../js/js_source_mapping.dart'; | 14 import '../js/js_source_mapping.dart'; |
15 import '../tokens/token.dart' show Token; | 15 import '../tokens/token.dart' show Token; |
16 import '../tree/tree.dart' show Node; | 16 import '../tree/tree.dart' show Node; |
17 import 'source_file.dart'; | 17 import 'source_file.dart'; |
18 import 'source_information.dart'; | 18 import 'source_information.dart'; |
19 | 19 |
20 /// Source information that contains start source position and optionally an | 20 /// Source information that contains start source position and optionally an |
21 /// end source position. | 21 /// end source position. |
22 class StartEndSourceInformation extends SourceInformation { | 22 class StartEndSourceInformation extends SourceInformation { |
(...skipping 30 matching lines...) Expand all Loading... |
53 bool operator ==(other) { | 53 bool operator ==(other) { |
54 if (identical(this, other)) return true; | 54 if (identical(this, other)) return true; |
55 if (other is! StartEndSourceInformation) return false; | 55 if (other is! StartEndSourceInformation) return false; |
56 return startPosition == other.startPosition && | 56 return startPosition == other.startPosition && |
57 endPosition == other.endPosition; | 57 endPosition == other.endPosition; |
58 } | 58 } |
59 | 59 |
60 // TODO(johnniwinther): Inline this in | 60 // TODO(johnniwinther): Inline this in |
61 // [StartEndSourceInformationBuilder.buildDeclaration]. | 61 // [StartEndSourceInformationBuilder.buildDeclaration]. |
62 static StartEndSourceInformation _computeSourceInformation( | 62 static StartEndSourceInformation _computeSourceInformation( |
63 AstElement element) { | 63 ResolvedAst resolvedAst) { |
64 AstElement implementation = element.implementation; | 64 String name = computeElementNameForSourceMaps(resolvedAst.element); |
65 SourceFile sourceFile = implementation.compilationUnit.script.file; | 65 SourceFile sourceFile; |
66 String name = computeElementNameForSourceMaps(element); | 66 int begin; |
67 Node node = implementation.node; | 67 int end; |
68 Token beginToken; | 68 if (resolvedAst.kind != ResolvedAstKind.PARSED) { |
69 Token endToken; | |
70 if (node == null) { | |
71 // Synthesized node. Use the enclosing element for the location. | 69 // Synthesized node. Use the enclosing element for the location. |
72 beginToken = endToken = element.position; | 70 sourceFile = resolvedAst.element.compilationUnit.script.file; |
| 71 begin = end = resolvedAst.element.sourcePosition.begin; |
73 } else { | 72 } else { |
74 beginToken = node.getBeginToken(); | 73 AstElement implementation = resolvedAst.element.implementation; |
75 endToken = node.getEndToken(); | 74 sourceFile = implementation.compilationUnit.script.file; |
| 75 Node node = resolvedAst.node; |
| 76 begin = node.getBeginToken().charOffset; |
| 77 end = node.getEndToken().charOffset; |
76 } | 78 } |
77 // TODO(johnniwinther): find the right sourceFile here and remove offset | 79 // TODO(johnniwinther): find the right sourceFile here and remove offset |
78 // checks below. | 80 // checks below. |
79 SourceLocation sourcePosition, endSourcePosition; | 81 SourceLocation sourcePosition, endSourcePosition; |
80 if (beginToken.charOffset < sourceFile.length) { | 82 if (begin < sourceFile.length) { |
81 sourcePosition = | 83 sourcePosition = |
82 new OffsetSourceLocation(sourceFile, beginToken.charOffset, name); | 84 new OffsetSourceLocation(sourceFile, begin, name); |
83 } | 85 } |
84 if (endToken.charOffset < sourceFile.length) { | 86 if (end < sourceFile.length) { |
85 endSourcePosition = | 87 endSourcePosition = new OffsetSourceLocation(sourceFile, end, name); |
86 new OffsetSourceLocation(sourceFile, endToken.charOffset, name); | |
87 } | 88 } |
88 return new StartEndSourceInformation(sourcePosition, endSourcePosition); | 89 return new StartEndSourceInformation(sourcePosition, endSourcePosition); |
89 } | 90 } |
90 | 91 |
91 /// Create a textual representation of the source information using [uriText] | 92 /// Create a textual representation of the source information using [uriText] |
92 /// as the Uri representation. | 93 /// as the Uri representation. |
93 String _computeText(String uriText) { | 94 String _computeText(String uriText) { |
94 StringBuffer sb = new StringBuffer(); | 95 StringBuffer sb = new StringBuffer(); |
95 sb.write('$uriText:'); | 96 sb.write('$uriText:'); |
96 // Use 1-based line/startPosition info to match usual dart tool output. | 97 // Use 1-based line/startPosition info to match usual dart tool output. |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 155 |
155 /// [SourceInformationBuilder] that generates [PositionSourceInformation]. | 156 /// [SourceInformationBuilder] that generates [PositionSourceInformation]. |
156 class StartEndSourceInformationBuilder extends SourceInformationBuilder { | 157 class StartEndSourceInformationBuilder extends SourceInformationBuilder { |
157 final SourceFile sourceFile; | 158 final SourceFile sourceFile; |
158 final String name; | 159 final String name; |
159 | 160 |
160 StartEndSourceInformationBuilder(AstElement element) | 161 StartEndSourceInformationBuilder(AstElement element) |
161 : sourceFile = element.compilationUnit.script.file, | 162 : sourceFile = element.compilationUnit.script.file, |
162 name = computeElementNameForSourceMaps(element); | 163 name = computeElementNameForSourceMaps(element); |
163 | 164 |
164 SourceInformation buildDeclaration(AstElement element) { | 165 SourceInformation buildDeclaration(ResolvedAst resolvedAst) { |
165 return StartEndSourceInformation._computeSourceInformation(element); | 166 return StartEndSourceInformation._computeSourceInformation(resolvedAst); |
166 } | 167 } |
167 | 168 |
168 SourceLocation sourceFileLocationForToken(Token token) { | 169 SourceLocation sourceFileLocationForToken(Token token) { |
169 SourceLocation location = | 170 SourceLocation location = |
170 new OffsetSourceLocation(sourceFile, token.charOffset, name); | 171 new OffsetSourceLocation(sourceFile, token.charOffset, name); |
171 checkValidSourceFileLocation(location, sourceFile, token.charOffset); | 172 checkValidSourceFileLocation(location, sourceFile, token.charOffset); |
172 return location; | 173 return location; |
173 } | 174 } |
174 | 175 |
175 void checkValidSourceFileLocation( | 176 void checkValidSourceFileLocation( |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 217 |
217 @override | 218 @override |
218 SourceInformation buildIf(Node node) => buildGeneric(node); | 219 SourceInformation buildIf(Node node) => buildGeneric(node); |
219 | 220 |
220 @override | 221 @override |
221 SourceInformationBuilder forContext(AstElement element, | 222 SourceInformationBuilder forContext(AstElement element, |
222 {SourceInformation sourceInformation}) { | 223 {SourceInformation sourceInformation}) { |
223 return new StartEndSourceInformationBuilder(element); | 224 return new StartEndSourceInformationBuilder(element); |
224 } | 225 } |
225 } | 226 } |
OLD | NEW |