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 code_generator; | 5 library code_generator; |
6 | 6 |
7 import 'glue.dart'; | 7 import 'glue.dart'; |
8 | 8 |
9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; | 9 import '../../tree_ir/tree_ir_nodes.dart' as tree_ir; |
10 import '../../js/js.dart' as js; | 10 import '../../js/js.dart' as js; |
11 import '../../elements/elements.dart'; | 11 import '../../elements/elements.dart'; |
12 import '../../io/source_information.dart' show SourceInformation; | 12 import '../../io/source_information.dart' show SourceInformation; |
13 import '../../util/maplet.dart'; | 13 import '../../util/maplet.dart'; |
14 import '../../constants/values.dart'; | 14 import '../../constants/values.dart'; |
15 import '../../dart2jslib.dart'; | 15 import '../../dart2jslib.dart'; |
| 16 import '../../dart_types.dart'; |
| 17 |
| 18 part 'type_test_emitter.dart'; |
16 | 19 |
17 class CodegenBailout { | 20 class CodegenBailout { |
18 final tree_ir.Node node; | 21 final tree_ir.Node node; |
19 final String reason; | 22 final String reason; |
20 CodegenBailout(this.node, this.reason); | 23 CodegenBailout(this.node, this.reason); |
21 String get message { | 24 String get message { |
22 return 'bailout${node != null ? " on $node" : ""}: $reason'; | 25 return 'bailout${node != null ? " on $node" : ""}: $reason'; |
23 } | 26 } |
24 } | 27 } |
25 | 28 |
26 class CodeGenerator extends tree_ir.StatementVisitor | 29 class CodeGenerator extends tree_ir.StatementVisitor |
27 with tree_ir.ExpressionVisitor<js.Expression> { | 30 with tree_ir.ExpressionVisitor<js.Expression>, |
| 31 TypeTestEmitter { |
28 final CodegenRegistry registry; | 32 final CodegenRegistry registry; |
29 | 33 |
30 final Glue glue; | 34 final Glue glue; |
31 | 35 |
32 ExecutableElement currentFunction; | 36 ExecutableElement currentFunction; |
33 | 37 |
34 /// Maps variables to their name. | 38 /// Maps variables to their name. |
35 Map<tree_ir.Variable, String> variableNames = <tree_ir.Variable, String>{}; | 39 Map<tree_ir.Variable, String> variableNames = <tree_ir.Variable, String>{}; |
36 | 40 |
37 /// Maps local constants to their name. | 41 /// Maps local constants to their name. |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
374 return new js.Prefix("!", visitExpression(node.operand)); | 378 return new js.Prefix("!", visitExpression(node.operand)); |
375 } | 379 } |
376 | 380 |
377 @override | 381 @override |
378 js.Expression visitThis(tree_ir.This node) { | 382 js.Expression visitThis(tree_ir.This node) { |
379 return new js.This(); | 383 return new js.This(); |
380 } | 384 } |
381 | 385 |
382 @override | 386 @override |
383 js.Expression visitTypeOperator(tree_ir.TypeOperator node) { | 387 js.Expression visitTypeOperator(tree_ir.TypeOperator node) { |
384 return giveup(node); | 388 if (!node.isTypeTest) { |
385 // TODO: implement visitTypeOperator | 389 giveup(node, 'type casts not implemented.'); |
| 390 } |
| 391 DartType type = node.type; |
| 392 if (type is InterfaceType && type.typeArguments.isEmpty) { |
| 393 glue.registerIsCheck(type, registry); |
| 394 js.Expression value = visitExpression(node.receiver); |
| 395 return emitSubtypeTest(node, value, type); |
| 396 } |
| 397 return giveup(node, 'type check unimplemented for $type.'); |
386 } | 398 } |
387 | 399 |
388 @override | 400 @override |
389 js.Expression visitVariableUse(tree_ir.VariableUse node) { | 401 js.Expression visitVariableUse(tree_ir.VariableUse node) { |
390 return buildVariableAccess(node.variable); | 402 return buildVariableAccess(node.variable); |
391 } | 403 } |
392 | 404 |
393 js.Expression buildVariableAccess(tree_ir.Variable variable) { | 405 js.Expression buildVariableAccess(tree_ir.Variable variable) { |
394 return new js.VariableUse(getVariableName(variable)); | 406 return new js.VariableUse(getVariableName(variable)); |
395 } | 407 } |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
650 | 662 |
651 @override | 663 @override |
652 visitVariableDeclaration(tree_ir.VariableDeclaration node) { | 664 visitVariableDeclaration(tree_ir.VariableDeclaration node) { |
653 return errorUnsupportedNode(node); | 665 return errorUnsupportedNode(node); |
654 } | 666 } |
655 | 667 |
656 errorUnsupportedNode(tree_ir.DartSpecificNode node) { | 668 errorUnsupportedNode(tree_ir.DartSpecificNode node) { |
657 throw "Unsupported node in JS backend: $node"; | 669 throw "Unsupported node in JS backend: $node"; |
658 } | 670 } |
659 } | 671 } |
OLD | NEW |