OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 /// Source information system mapping that attempts a semantic mapping between | |
6 /// offsets of JavaScript code points to offsets of Dart code points. | |
7 | |
8 library dart2js.source_information.position; | |
9 | |
10 import '../dart2jslib.dart' show | |
11 invariant, | |
12 MessageKind, | |
13 SourceSpan; | |
14 import '../elements/elements.dart' show | |
15 AstElement, | |
16 LocalElement; | |
17 import '../js/js.dart' as js; | |
18 import '../js/js_source_mapping.dart'; | |
19 import '../js/js_debug.dart'; | |
20 import '../tree/tree.dart' show Node, Send; | |
21 import '../util/util.dart' show NO_LOCATION_SPANNABLE; | |
22 | |
23 import 'source_file.dart'; | |
24 import 'source_information.dart'; | |
25 | |
26 /// [SourceInformation] that consists of an offset position into the source | |
27 /// code. | |
28 class PositionSourceInformation extends SourceInformation { | |
29 @override | |
30 final SourceLocation startPosition; | |
31 | |
32 @override | |
33 final SourceLocation closingPosition; | |
34 | |
35 PositionSourceInformation(this.startPosition, | |
36 [this.closingPosition]); | |
37 | |
38 @override | |
39 List<SourceLocation> get sourceLocations { | |
40 List<SourceLocation> list = <SourceLocation>[]; | |
41 if (startPosition != null) { | |
42 list.add(startPosition); | |
43 } | |
44 if (closingPosition != null) { | |
45 list.add(closingPosition); | |
46 } | |
47 return list; | |
48 } | |
49 | |
50 @override | |
51 SourceSpan get sourceSpan { | |
52 SourceLocation location = | |
53 startPosition != null ? startPosition : closingPosition; | |
54 Uri uri = location.sourceUri; | |
55 int offset = location.offset; | |
56 return new SourceSpan(uri, offset, offset); | |
57 } | |
58 | |
59 int get hashCode { | |
60 return 0x7FFFFFFF & | |
61 (startPosition.hashCode * 17 + closingPosition.hashCode * 19); | |
62 } | |
63 | |
64 bool operator ==(other) { | |
65 if (identical(this, other)) return true; | |
66 if (other is! PositionSourceInformation) return false; | |
67 return startPosition == other.startPosition && | |
68 closingPosition == other.closingPosition; | |
69 } | |
70 | |
71 /// Create a textual representation of the source information using [uriText] | |
72 /// as the Uri representation. | |
73 String _computeText(String uriText) { | |
74 StringBuffer sb = new StringBuffer(); | |
75 sb.write('$uriText:'); | |
76 // Use 1-based line/column info to match usual dart tool output. | |
77 if (startPosition != null) { | |
78 sb.write('[${startPosition.line + 1},' | |
79 '${startPosition.column + 1}]'); | |
80 } | |
81 if (closingPosition != null) { | |
82 sb.write('-[${closingPosition.line + 1},' | |
83 '${closingPosition.column + 1}]'); | |
84 } | |
85 return sb.toString(); | |
86 } | |
87 | |
88 String get shortText { | |
89 if (startPosition != null) { | |
90 return _computeText(startPosition.sourceUri.pathSegments.last); | |
91 } else { | |
92 return _computeText(closingPosition.sourceUri.pathSegments.last); | |
93 } | |
94 } | |
95 | |
96 String toString() { | |
97 if (startPosition != null) { | |
98 return _computeText('${startPosition.sourceUri}'); | |
99 } else { | |
100 return _computeText('${closingPosition.sourceUri}'); | |
101 } | |
102 } | |
103 } | |
104 | |
105 class PositionSourceInformationStrategy | |
106 implements JavaScriptSourceInformationStrategy { | |
107 const PositionSourceInformationStrategy(); | |
108 | |
109 @override | |
110 SourceInformationBuilder createBuilderForContext(AstElement element) { | |
111 return new PositionSourceInformationBuilder(element); | |
112 } | |
113 | |
114 @override | |
115 SourceInformationProcessor createProcessor(SourceMapper mapper) { | |
116 return new PositionSourceInformationProcessor(mapper); | |
117 } | |
118 } | |
119 | |
120 /// [SourceInformationBuilder] that generates [PositionSourceInformation]. | |
121 class PositionSourceInformationBuilder implements SourceInformationBuilder { | |
122 final SourceFile sourceFile; | |
123 final String name; | |
124 | |
125 PositionSourceInformationBuilder(AstElement element) | |
126 : sourceFile = element.implementation.compilationUnit.script.file, | |
127 name = computeElementNameForSourceMaps(element); | |
128 | |
129 SourceInformation buildDeclaration(AstElement element) { | |
130 if (element.isSynthesized) { | |
131 return new PositionSourceInformation( | |
132 new OffsetSourceLocation( | |
133 sourceFile, element.position.charOffset, name)); | |
134 } else { | |
135 return new PositionSourceInformation( | |
136 null, | |
137 new OffsetSourceLocation(sourceFile, | |
138 element.resolvedAst.node.getEndToken().charOffset, name)); | |
139 } | |
140 } | |
141 | |
142 /// Builds a source information object pointing the start position of [node]. | |
143 SourceInformation buildBegin(Node node) { | |
144 return new PositionSourceInformation(new OffsetSourceLocation( | |
145 sourceFile, node.getBeginToken().charOffset, name)); | |
146 } | |
147 | |
148 @override | |
149 SourceInformation buildGeneric(Node node) => buildBegin(node); | |
150 | |
151 @override | |
152 SourceInformation buildReturn(Node node) => buildBegin(node); | |
153 | |
154 @override | |
155 SourceInformation buildImplicitReturn(AstElement element) { | |
156 if (element.isSynthesized) { | |
157 return new PositionSourceInformation( | |
158 new OffsetSourceLocation( | |
159 sourceFile, element.position.charOffset, name)); | |
160 } else { | |
161 return new PositionSourceInformation( | |
162 new OffsetSourceLocation(sourceFile, | |
163 element.resolvedAst.node.getEndToken().charOffset, name)); | |
164 } | |
165 } | |
166 | |
167 | |
168 @override | |
169 SourceInformation buildLoop(Node node) => buildBegin(node); | |
170 | |
171 @override | |
172 SourceInformation buildGet(Node node) => buildBegin(node); | |
173 | |
174 @override | |
175 SourceInformation buildCall(Node receiver, Node call) { | |
176 return new PositionSourceInformation( | |
177 new OffsetSourceLocation( | |
178 sourceFile, receiver.getBeginToken().charOffset, name), | |
179 new OffsetSourceLocation( | |
180 sourceFile, call.getBeginToken().charOffset, name)); | |
181 } | |
182 | |
183 @override | |
184 SourceInformation buildNew(Node node) { | |
185 return buildBegin(node); | |
186 } | |
187 | |
188 @override | |
189 SourceInformation buildIf(Node node) => buildBegin(node); | |
190 | |
191 @override | |
192 SourceInformation buildThrow(Node node) => buildBegin(node); | |
193 | |
194 @override | |
195 SourceInformation buildAssignment(Node node) => buildBegin(node); | |
196 | |
197 @override | |
198 SourceInformationBuilder forContext(AstElement element) { | |
199 return new PositionSourceInformationBuilder(element); | |
200 } | |
201 } | |
202 | |
203 /// The start, end and closing offsets for a [js.Node]. | |
204 class CodePosition { | |
205 final int startPosition; | |
206 final int endPosition; | |
207 final int closingPosition; | |
208 | |
209 CodePosition(this.startPosition, this.endPosition, this.closingPosition); | |
210 } | |
211 | |
212 /// Registry for mapping [js.Node]s to their [CodePosition]. | |
213 class CodePositionRecorder { | |
214 Map<js.Node, CodePosition> _codePositionMap = <js.Node, CodePosition>{}; | |
215 | |
216 void registerPositions(js.Node node, | |
217 int startPosition, | |
218 int endPosition, | |
219 int closingPosition) { | |
220 registerCodePosition(node, | |
221 new CodePosition(startPosition, endPosition, closingPosition)); | |
222 } | |
223 | |
224 void registerCodePosition(js.Node node, CodePosition codePosition) { | |
225 _codePositionMap[node] = codePosition; | |
226 } | |
227 | |
228 CodePosition operator [](js.Node node) => _codePositionMap[node]; | |
229 } | |
230 | |
231 enum SourcePositionKind { | |
232 START, | |
233 CLOSING, | |
234 END, | |
235 } | |
236 | |
237 enum CodePositionKind { | |
238 START, | |
239 CLOSING, | |
240 END, | |
241 } | |
242 | |
243 /// Processor that associates [SourceLocation]s from [SourceInformation] on | |
244 /// [js.Node]s with the target offsets in a [SourceMapper]. | |
245 class PositionSourceInformationProcessor | |
246 extends js.BaseVisitor implements SourceInformationProcessor { | |
247 final CodePositionRecorder codePositions = new CodePositionRecorder(); | |
248 final SourceMapper sourceMapper; | |
249 | |
250 PositionSourceInformationProcessor(this.sourceMapper); | |
251 | |
252 void process(js.Node node) { | |
253 node.accept(this); | |
254 } | |
255 | |
256 void visitChildren(js.Node node) { | |
257 node.visitChildren(this); | |
258 } | |
259 | |
260 CodePosition getCodePosition(js.Node node) { | |
261 return codePositions[node]; | |
262 } | |
263 | |
264 /// Associates [sourceInformation] with [node] the code positions of | |
floitsch
2015/06/29 16:30:44
Sentence doesn't make sense to me.
Maybe make it
Johnni Winther
2015/07/02 08:38:09
Done.
| |
265 /// using [codePositionNode] and [codePositionKind] to pick the code offset | |
266 /// and [sourcePositionKind] to pick the source location. | |
267 void apply(js.Node node, | |
268 js.Node codePositionNode, | |
269 CodePositionKind codePositionKind, | |
270 SourceInformation sourceInformation, | |
271 SourcePositionKind sourcePositionKind) { | |
272 if (sourceInformation != null) { | |
273 CodePosition codePosition = getCodePosition(codePositionNode); | |
274 // We should always have recorded the needed code positions. | |
275 assert(invariant( | |
276 NO_LOCATION_SPANNABLE, | |
277 codePosition != null, | |
278 message: | |
279 "Code position missing for " | |
280 "${nodeToString(codePositionNode)}:\n" | |
281 "${DebugPrinter.prettyPrint(node)}")); | |
282 if (codePosition == null) return; | |
283 int codeLocation; | |
284 SourceLocation sourceLocation; | |
285 switch (codePositionKind) { | |
286 case CodePositionKind.START: | |
287 codeLocation = codePosition.startPosition; | |
288 break; | |
289 case CodePositionKind.CLOSING: | |
290 codeLocation = codePosition.closingPosition; | |
291 break; | |
292 case CodePositionKind.END: | |
293 codeLocation = codePosition.endPosition; | |
294 break; | |
295 } | |
296 switch (sourcePositionKind) { | |
297 case SourcePositionKind.START: | |
298 sourceLocation = sourceInformation.startPosition; | |
299 break; | |
300 case SourcePositionKind.CLOSING: | |
301 sourceLocation = sourceInformation.closingPosition; | |
302 break; | |
303 case SourcePositionKind.END: | |
304 sourceLocation = sourceInformation.endPosition; | |
305 break; | |
306 } | |
307 if (codeLocation != null && sourceLocation != null) { | |
308 sourceMapper.register(node, codeLocation, sourceLocation); | |
309 } | |
310 } | |
311 } | |
312 | |
313 @override | |
314 visitNode(js.Node node) { | |
315 SourceInformation sourceInformation = node.sourceInformation; | |
316 if (sourceInformation != null) { | |
317 /// Associates the left-most position of the JS code with the left-most | |
318 /// position of the Dart code. | |
319 apply(node, | |
320 node, CodePositionKind.START, | |
321 sourceInformation, SourcePositionKind.START); | |
322 } | |
323 visitChildren(node); | |
324 } | |
325 | |
326 @override | |
327 visitFun(js.Fun node) { | |
328 SourceInformation sourceInformation = node.sourceInformation; | |
329 if (sourceInformation != null) { | |
330 /// Associates the end brace of the JavaScript function with the end brace | |
331 /// of the Dart function (or the `;` in case of arrow notation). | |
332 apply(node, | |
333 node, CodePositionKind.CLOSING, | |
334 sourceInformation, SourcePositionKind.CLOSING); | |
335 } | |
336 | |
337 visitChildren(node); | |
338 } | |
339 | |
340 @override | |
341 visitExpressionStatement(js.ExpressionStatement node) { | |
342 visitChildren(node); | |
343 } | |
344 | |
345 @override | |
346 visitBinary(js.Binary node) { | |
347 visitChildren(node); | |
348 } | |
349 | |
350 @override | |
351 visitAccess(js.PropertyAccess node) { | |
352 visitChildren(node); | |
353 } | |
354 | |
355 @override | |
356 visitCall(js.Call node) { | |
357 SourceInformation sourceInformation = node.sourceInformation; | |
358 if (sourceInformation != null) { | |
359 if (node.target is js.PropertyAccess) { | |
360 js.PropertyAccess access = node.target; | |
361 js.Node target = access; | |
362 bool pureAccess = false; | |
363 while (target is js.PropertyAccess) { | |
364 js.PropertyAccess targetAccess = target; | |
365 if (targetAccess.receiver is js.VariableUse || | |
366 targetAccess.receiver is js.This) { | |
367 pureAccess = true; | |
368 break; | |
369 } else { | |
370 target = targetAccess.receiver; | |
371 } | |
372 } | |
373 if (pureAccess) { | |
374 // a.m() this.m() a.b.c.d.m() | |
375 // ^ ^ ^ | |
376 apply( | |
377 node, | |
378 node, | |
379 CodePositionKind.START, | |
380 sourceInformation, | |
381 SourcePositionKind.START); | |
382 } else { | |
383 // *.m() *.a.b.c.d.m() | |
384 // ^ ^ | |
385 apply( | |
386 node, | |
387 access.selector, | |
388 CodePositionKind.START, | |
389 sourceInformation, | |
390 SourcePositionKind.CLOSING); | |
391 } | |
392 } else if (node.target is js.VariableUse) { | |
393 // m() | |
394 // ^ | |
395 apply( | |
396 node, | |
397 node, | |
398 CodePositionKind.START, | |
399 sourceInformation, | |
400 SourcePositionKind.START); | |
401 } else if (node.target is js.Fun || node.target is js.New) { | |
402 // function(){}() new Function("...")() | |
403 // ^ ^ | |
404 apply( | |
405 node, | |
406 node.target, | |
407 CodePositionKind.END, | |
408 sourceInformation, | |
409 SourcePositionKind.CLOSING); | |
410 } else { | |
411 assert(invariant(NO_LOCATION_SPANNABLE, false, | |
412 message: "Unexpected property access ${nodeToString(node)}:\n" | |
413 "${DebugPrinter.prettyPrint(node)}")); | |
414 // Don't know.... | |
415 apply( | |
416 node, | |
417 node, | |
418 CodePositionKind.START, | |
419 sourceInformation, | |
420 SourcePositionKind.START); | |
421 } | |
422 } | |
423 visitChildren(node); | |
424 } | |
425 | |
426 @override | |
427 void onPositions(js.Node node, | |
428 int startPosition, | |
429 int endPosition, | |
430 int closingPosition) { | |
431 codePositions.registerPositions( | |
432 node, startPosition, endPosition, closingPosition); | |
433 } | |
434 } | |
OLD | NEW |