| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.ir_builder; | 5 library dart2js.ir_builder; |
| 6 | 6 |
| 7 import '../constants/expressions.dart'; | 7 import '../constants/expressions.dart'; |
| 8 import '../constants/values.dart' show PrimitiveConstantValue; | 8 import '../constants/values.dart' show PrimitiveConstantValue; |
| 9 import '../dart_backend/dart_backend.dart' show DartBackend; | 9 import '../dart_backend/dart_backend.dart' show DartBackend; |
| 10 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 ir.InvokeContinuation invoke = new ir.InvokeContinuation.uninitialized(); | 120 ir.InvokeContinuation invoke = new ir.InvokeContinuation.uninitialized(); |
| 121 builder.add(invoke); | 121 builder.add(invoke); |
| 122 _invocations.add(invoke); | 122 _invocations.add(invoke); |
| 123 _environments.add(builder.environment); | 123 _environments.add(builder.environment); |
| 124 builder._current = null; | 124 builder._current = null; |
| 125 // TODO(kmillikin): Can we set builder.environment to null to make it | 125 // TODO(kmillikin): Can we set builder.environment to null to make it |
| 126 // less likely to mutate it? | 126 // less likely to mutate it? |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 /// Function for building a node in the context of the current builder. |
| 131 typedef ir.Node BuildFunction(node); |
| 132 |
| 130 /// Function for building nodes in the context of the provided [builder]. | 133 /// Function for building nodes in the context of the provided [builder]. |
| 131 typedef ir.Node SubbuildFunction(IrBuilder builder); | 134 typedef ir.Node SubbuildFunction(IrBuilder builder); |
| 132 | 135 |
| 133 /// Mixin that provides encapsulated access to nested builders. | 136 /// Mixin that provides encapsulated access to nested builders. |
| 134 abstract class IrBuilderMixin<N> { | 137 abstract class IrBuilderMixin<N> { |
| 135 IrBuilder _irBuilder; | 138 IrBuilder _irBuilder; |
| 136 | 139 |
| 137 /// Execute [f] with [builder] as the current builder. | 140 /// Execute [f] with [builder] as the current builder. |
| 138 withBuilder(IrBuilder builder, f()) { | 141 withBuilder(IrBuilder builder, f()) { |
| 139 assert(builder != null); | 142 assert(builder != null); |
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 ir.Constant buildNullLiteral() { | 383 ir.Constant buildNullLiteral() { |
| 381 return _buildPrimitiveConstant(state.constantSystem.createNull()); | 384 return _buildPrimitiveConstant(state.constantSystem.createNull()); |
| 382 } | 385 } |
| 383 | 386 |
| 384 /// Create a string literal. | 387 /// Create a string literal. |
| 385 ir.Constant buildStringLiteral(String value) { | 388 ir.Constant buildStringLiteral(String value) { |
| 386 return _buildPrimitiveConstant( | 389 return _buildPrimitiveConstant( |
| 387 state.constantSystem.createString(new ast.DartString.literal(value))); | 390 state.constantSystem.createString(new ast.DartString.literal(value))); |
| 388 } | 391 } |
| 389 | 392 |
| 393 /// Creates a non-constant list literal of the provided [type] and with the |
| 394 /// provided [values]. |
| 395 ir.Primitive buildListLiteral(InterfaceType type, |
| 396 Iterable<ir.Primitive> values) { |
| 397 assert(isOpen); |
| 398 ir.Primitive result = new ir.LiteralList(type, values); |
| 399 add(new ir.LetPrim(result)); |
| 400 return result; |
| 401 } |
| 402 |
| 403 /// Creates a non-constant map literal of the provided [type] and with the |
| 404 /// entries build from the [keys] and [values] using [build]. |
| 405 ir.Primitive buildMapLiteral(InterfaceType type, |
| 406 Iterable keys, |
| 407 Iterable values, |
| 408 BuildFunction build) { |
| 409 assert(isOpen); |
| 410 List<ir.LiteralMapEntry> entries = <ir.LiteralMapEntry>[]; |
| 411 Iterator key = keys.iterator; |
| 412 Iterator value = values.iterator; |
| 413 while (key.moveNext() && value.moveNext()) { |
| 414 entries.add(new ir.LiteralMapEntry( |
| 415 build(key.current), build(value.current))); |
| 416 } |
| 417 assert(!key.moveNext() && !value.moveNext()); |
| 418 ir.Primitive result = new ir.LiteralMap(type, entries); |
| 419 add(new ir.LetPrim(result)); |
| 420 return result; |
| 421 } |
| 422 |
| 390 /// Creates a conditional expression with the provided [condition] where the | 423 /// Creates a conditional expression with the provided [condition] where the |
| 391 /// then and else expression are created through the [buildThenExpression] and | 424 /// then and else expression are created through the [buildThenExpression] and |
| 392 /// [buildElseExpression] functions, respectively. | 425 /// [buildElseExpression] functions, respectively. |
| 393 ir.Primitive buildConditional( | 426 ir.Primitive buildConditional( |
| 394 ir.Primitive condition, | 427 ir.Primitive condition, |
| 395 ir.Primitive buildThenExpression(IrBuilder builder), | 428 ir.Primitive buildThenExpression(IrBuilder builder), |
| 396 ir.Primitive buildElseExpression(IrBuilder builder)) { | 429 ir.Primitive buildElseExpression(IrBuilder builder)) { |
| 397 | 430 |
| 398 assert(isOpen); | 431 assert(isOpen); |
| 399 | 432 |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 value = buildNullLiteral(); | 822 value = buildNullLiteral(); |
| 790 } | 823 } |
| 791 add(new ir.InvokeContinuation(state.returnContinuation, [value])); | 824 add(new ir.InvokeContinuation(state.returnContinuation, [value])); |
| 792 _current = null; | 825 _current = null; |
| 793 } | 826 } |
| 794 | 827 |
| 795 /// Create a blocks of [statements] by applying [build] to all reachable | 828 /// Create a blocks of [statements] by applying [build] to all reachable |
| 796 /// statements. The first statement is assumed to be reachable. | 829 /// statements. The first statement is assumed to be reachable. |
| 797 // TODO(johnniwinther): Type [statements] as `Iterable` when `NodeList` uses | 830 // TODO(johnniwinther): Type [statements] as `Iterable` when `NodeList` uses |
| 798 // `List` instead of `Link`. | 831 // `List` instead of `Link`. |
| 799 void buildBlock(var statements, build(statement)) { | 832 void buildBlock(var statements, BuildFunction build) { |
| 800 // Build(Block(stamements), C) = C' | 833 // Build(Block(stamements), C) = C' |
| 801 // where C' = statements.fold(Build, C) | 834 // where C' = statements.fold(Build, C) |
| 802 assert(isOpen); | 835 assert(isOpen); |
| 803 return buildSequence(statements, build); | 836 return buildSequence(statements, build); |
| 804 } | 837 } |
| 805 | 838 |
| 806 /// Creates a sequence of [nodes] by applying [build] to all reachable nodes. | 839 /// Creates a sequence of [nodes] by applying [build] to all reachable nodes. |
| 807 /// | 840 /// |
| 808 /// The first node in the sequence does not need to be reachable. | 841 /// The first node in the sequence does not need to be reachable. |
| 809 // TODO(johnniwinther): Type [nodes] as `Iterable` when `NodeList` uses | 842 // TODO(johnniwinther): Type [nodes] as `Iterable` when `NodeList` uses |
| 810 // `List` instead of `Link`. | 843 // `List` instead of `Link`. |
| 811 void buildSequence(var nodes, build(node)) { | 844 void buildSequence(var nodes, BuildFunction build) { |
| 812 for (var node in nodes) { | 845 for (var node in nodes) { |
| 813 if (!isOpen) return; | 846 if (!isOpen) return; |
| 814 build(node); | 847 build(node); |
| 815 } | 848 } |
| 816 } | 849 } |
| 817 | 850 |
| 818 | 851 |
| 819 // Build(BreakStatement L, C) = C[InvokeContinuation(...)] | 852 // Build(BreakStatement L, C) = C[InvokeContinuation(...)] |
| 820 // | 853 // |
| 821 // The continuation and arguments are filled in later after translating | 854 // The continuation and arguments are filled in later after translating |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1060 index = 0; | 1093 index = 0; |
| 1061 for (int i = 0; i < environment.length; ++i) { | 1094 for (int i = 0; i < environment.length; ++i) { |
| 1062 if (common[i] == null) { | 1095 if (common[i] == null) { |
| 1063 environment.index2value[i] = parameters[index++]; | 1096 environment.index2value[i] = parameters[index++]; |
| 1064 } | 1097 } |
| 1065 } | 1098 } |
| 1066 | 1099 |
| 1067 return join; | 1100 return join; |
| 1068 } | 1101 } |
| 1069 } | 1102 } |
| OLD | NEW |