OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 dart_backend.tracer; | 5 library dart_backend.tracer; |
6 | 6 |
7 import 'dart:async' show EventSink; | 7 import 'dart:async' show EventSink; |
8 import '../tracer.dart'; | 8 import '../tracer.dart'; |
9 import 'dart_tree.dart'; | 9 import 'dart_tree.dart'; |
10 | 10 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 visitStatement(node.next); | 128 visitStatement(node.next); |
129 | 129 |
130 ifTargets[node.body] = bodyBlock; | 130 ifTargets[node.body] = bodyBlock; |
131 ifTargets[node.next] = nextBlock; | 131 ifTargets[node.next] = nextBlock; |
132 } | 132 } |
133 | 133 |
134 visitExpressionStatement(ExpressionStatement node) { | 134 visitExpressionStatement(ExpressionStatement node) { |
135 _addStatement(node); | 135 _addStatement(node); |
136 visitStatement(node.next); | 136 visitStatement(node.next); |
137 } | 137 } |
| 138 |
| 139 visitFunctionDeclaration(FunctionDeclaration node) { |
| 140 _addStatement(node); |
| 141 visitStatement(node.next); |
| 142 } |
138 } | 143 } |
139 | 144 |
140 class TreeTracer extends TracerUtil with StatementVisitor { | 145 class TreeTracer extends TracerUtil with StatementVisitor { |
141 final EventSink<String> output; | 146 final EventSink<String> output; |
142 | 147 |
143 TreeTracer(this.output); | 148 TreeTracer(this.output); |
144 | 149 |
145 Names names; | 150 Names names; |
146 BlockCollector collector; | 151 BlockCollector collector; |
147 int statementCounter; | 152 int statementCounter; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 String nextTarget = collector.ifTargets[node.next].name; | 248 String nextTarget = collector.ifTargets[node.next].name; |
244 printStatement(null, "while ${expr(node.condition)}"); | 249 printStatement(null, "while ${expr(node.condition)}"); |
245 printStatement(null, "do $bodyTarget"); | 250 printStatement(null, "do $bodyTarget"); |
246 printStatement(null, "then $nextTarget" ); | 251 printStatement(null, "then $nextTarget" ); |
247 } | 252 } |
248 | 253 |
249 visitExpressionStatement(ExpressionStatement node) { | 254 visitExpressionStatement(ExpressionStatement node) { |
250 printStatement(null, expr(node.expression)); | 255 printStatement(null, expr(node.expression)); |
251 } | 256 } |
252 | 257 |
| 258 visitFunctionDeclaration(FunctionDeclaration node) { |
| 259 printStatement(null, 'function ${node.definition.element.name}'); |
| 260 } |
| 261 |
253 String expr(Expression e) { | 262 String expr(Expression e) { |
254 return e.accept(new SubexpressionVisitor(names)); | 263 return e.accept(new SubexpressionVisitor(names)); |
255 } | 264 } |
256 } | 265 } |
257 | 266 |
258 class SubexpressionVisitor extends ExpressionVisitor<String> { | 267 class SubexpressionVisitor extends ExpressionVisitor<String> { |
259 Names names; | 268 Names names; |
260 | 269 |
261 SubexpressionVisitor(this.names); | 270 SubexpressionVisitor(this.names); |
262 | 271 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 | 340 |
332 String visitConstant(Constant node) { | 341 String visitConstant(Constant node) { |
333 return "${node.value}"; | 342 return "${node.value}"; |
334 } | 343 } |
335 | 344 |
336 String visitThis(This node) { | 345 String visitThis(This node) { |
337 return "this"; | 346 return "this"; |
338 } | 347 } |
339 | 348 |
340 String visitReifyTypeVar(ReifyTypeVar node) { | 349 String visitReifyTypeVar(ReifyTypeVar node) { |
341 return "typevar [${node.element.name}]"; | 350 return "typevar [${node.typeVariable.name}]"; |
342 } | 351 } |
343 | 352 |
344 bool usesInfixNotation(Expression node) { | 353 bool usesInfixNotation(Expression node) { |
345 return node is Conditional || node is LogicalOperator; | 354 return node is Conditional || node is LogicalOperator; |
346 } | 355 } |
347 | 356 |
348 String visitConditional(Conditional node) { | 357 String visitConditional(Conditional node) { |
349 String condition = visitExpression(node.condition); | 358 String condition = visitExpression(node.condition); |
350 String thenExpr = visitExpression(node.thenExpression); | 359 String thenExpr = visitExpression(node.thenExpression); |
351 String elseExpr = visitExpression(node.elseExpression); | 360 String elseExpr = visitExpression(node.elseExpression); |
(...skipping 19 matching lines...) Expand all Loading... |
371 } | 380 } |
372 | 381 |
373 String visitNot(Not node) { | 382 String visitNot(Not node) { |
374 String operand = visitExpression(node.operand); | 383 String operand = visitExpression(node.operand); |
375 if (usesInfixNotation(node.operand)) { | 384 if (usesInfixNotation(node.operand)) { |
376 operand = '($operand)'; | 385 operand = '($operand)'; |
377 } | 386 } |
378 return '!$operand'; | 387 return '!$operand'; |
379 } | 388 } |
380 | 389 |
| 390 String visitFunctionExpression(FunctionExpression node) { |
| 391 return "function ${node.definition.element.name}"; |
| 392 } |
| 393 |
381 } | 394 } |
382 | 395 |
383 /** | 396 /** |
384 * Invents (and remembers) names for Variables that do not have an associated | 397 * Invents (and remembers) names for Variables that do not have an associated |
385 * identifier. | 398 * identifier. |
386 * | 399 * |
387 * In case a variable is named v0, v1, etc, it may be assigned a different | 400 * In case a variable is named v0, v1, etc, it may be assigned a different |
388 * name to avoid clashing with a previously synthesized variable name. | 401 * name to avoid clashing with a previously synthesized variable name. |
389 */ | 402 */ |
390 class Names { | 403 class Names { |
391 final Map<Variable, String> _names = {}; | 404 final Map<Variable, String> _names = {}; |
392 final Set<String> _usedNames = new Set(); | 405 final Set<String> _usedNames = new Set(); |
393 int _counter = 0; | 406 int _counter = 0; |
394 | 407 |
395 String varName(Variable v) { | 408 String varName(Variable v) { |
396 String name = _names[v]; | 409 String name = _names[v]; |
397 if (name == null) { | 410 if (name == null) { |
398 String prefix = v.element == null ? 'v' : '${v.element.name}_'; | 411 String prefix = v.element == null ? 'v' : '${v.element.name}_'; |
399 while (name == null || _usedNames.contains(name)) { | 412 while (name == null || _usedNames.contains(name)) { |
400 name = "$prefix${_counter++}"; | 413 name = "$prefix${_counter++}"; |
401 } | 414 } |
402 _names[v] = name; | 415 _names[v] = name; |
403 _usedNames.add(name); | 416 _usedNames.add(name); |
404 } | 417 } |
405 return name; | 418 return name; |
406 } | 419 } |
407 } | 420 } |
OLD | NEW |