| 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 dart2js.constants.expressions; | 5 library dart2js.constants.expressions; |
| 6 | 6 |
| 7 import '../dart2jslib.dart' show assertDebugMode; | 7 import '../dart2jslib.dart' show assertDebugMode; |
| 8 import '../dart_types.dart'; | 8 import '../dart_types.dart'; |
| 9 import '../elements/elements.dart' show | 9 import '../elements/elements.dart' show |
| 10 Element, | 10 Element, |
| 11 FunctionElement, | 11 FunctionElement, |
| 12 VariableElement; | 12 VariableElement; |
| 13 import '../universe/universe.dart' show CallStructure; | 13 import '../universe/universe.dart' show Selector; |
| 14 import 'values.dart'; | 14 import 'values.dart'; |
| 15 | 15 |
| 16 /// An expression that is a compile-time constant. | 16 /// An expression that is a compile-time constant. |
| 17 /// | 17 /// |
| 18 /// Whereas [ConstantValue] represent a compile-time value, a | 18 /// Whereas [ConstantValue] represent a compile-time value, a |
| 19 /// [ConstantExpression] represents an expression for creating a constant. | 19 /// [ConstantExpression] represents an expression for creating a constant. |
| 20 /// | 20 /// |
| 21 /// There is no one-to-one mapping between [ConstantExpression] and | 21 /// There is no one-to-one mapping between [ConstantExpression] and |
| 22 /// [ConstantValue], because different expressions can denote the same constant. | 22 /// [ConstantValue], because different expressions can denote the same constant. |
| 23 /// For instance, multiple `const` constructors may be used to create the same | 23 /// For instance, multiple `const` constructors may be used to create the same |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 accept(ConstantExpressionVisitor visitor, [context]) { | 94 accept(ConstantExpressionVisitor visitor, [context]) { |
| 95 return visitor.visitMap(this, context); | 95 return visitor.visitMap(this, context); |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 /// Invocation of a const constructor. | 99 /// Invocation of a const constructor. |
| 100 class ConstructedConstantExpression extends ConstantExpression { | 100 class ConstructedConstantExpression extends ConstantExpression { |
| 101 final ConstantValue value; | 101 final ConstantValue value; |
| 102 final InterfaceType type; | 102 final InterfaceType type; |
| 103 final FunctionElement target; | 103 final FunctionElement target; |
| 104 final CallStructure callStructure; | 104 final Selector selector; |
| 105 final List<ConstantExpression> arguments; | 105 final List<ConstantExpression> arguments; |
| 106 | 106 |
| 107 ConstructedConstantExpression( | 107 ConstructedConstantExpression(this.value, |
| 108 this.value, | 108 this.type, |
| 109 this.type, | 109 this.target, |
| 110 this.target, | 110 this.selector, |
| 111 this.callStructure, | 111 this.arguments) { |
| 112 this.arguments) { | |
| 113 assert(type.element == target.enclosingClass); | 112 assert(type.element == target.enclosingClass); |
| 114 } | 113 } |
| 115 | 114 |
| 116 accept(ConstantExpressionVisitor visitor, [context]) { | 115 accept(ConstantExpressionVisitor visitor, [context]) { |
| 117 return visitor.visitConstructed(this, context); | 116 return visitor.visitConstructed(this, context); |
| 118 } | 117 } |
| 119 } | 118 } |
| 120 | 119 |
| 121 /// String literal with juxtaposition and/or interpolations. | 120 /// String literal with juxtaposition and/or interpolations. |
| 122 // TODO(johnniwinther): Do we need this? | 121 // TODO(johnniwinther): Do we need this? |
| (...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 sb.write('const '); | 371 sb.write('const '); |
| 373 sb.write(exp.target.enclosingClass.name); | 372 sb.write(exp.target.enclosingClass.name); |
| 374 if (exp.target.name != '') { | 373 if (exp.target.name != '') { |
| 375 sb.write('.'); | 374 sb.write('.'); |
| 376 sb.write(exp.target.name); | 375 sb.write(exp.target.name); |
| 377 } | 376 } |
| 378 writeTypeArguments(exp.type); | 377 writeTypeArguments(exp.type); |
| 379 sb.write('('); | 378 sb.write('('); |
| 380 bool needsComma = false; | 379 bool needsComma = false; |
| 381 | 380 |
| 382 int namedOffset = exp.callStructure.positionalArgumentCount; | 381 int namedOffset = exp.selector.positionalArgumentCount; |
| 383 for (int index = 0; index < namedOffset; index++) { | 382 for (int index = 0; index < namedOffset; index++) { |
| 384 if (needsComma) { | 383 if (needsComma) { |
| 385 sb.write(', '); | 384 sb.write(', '); |
| 386 } | 385 } |
| 387 visit(exp.arguments[index]); | 386 visit(exp.arguments[index]); |
| 388 needsComma = true; | 387 needsComma = true; |
| 389 } | 388 } |
| 390 for (int index = 0; index < exp.callStructure.namedArgumentCount; index++) { | 389 for (int index = 0; index < exp.selector.namedArgumentCount; index++) { |
| 391 if (needsComma) { | 390 if (needsComma) { |
| 392 sb.write(', '); | 391 sb.write(', '); |
| 393 } | 392 } |
| 394 sb.write(exp.callStructure.namedArguments[index]); | 393 sb.write(exp.selector.namedArguments[index]); |
| 395 sb.write(': '); | 394 sb.write(': '); |
| 396 visit(exp.arguments[namedOffset + index]); | 395 visit(exp.arguments[namedOffset + index]); |
| 397 needsComma = true; | 396 needsComma = true; |
| 398 } | 397 } |
| 399 sb.write(')'); | 398 sb.write(')'); |
| 400 } | 399 } |
| 401 | 400 |
| 402 @override | 401 @override |
| 403 void visitConcatenate(ConcatenateConstantExpression exp, [_]) { | 402 void visitConcatenate(ConcatenateConstantExpression exp, [_]) { |
| 404 sb.write(exp.value.unparse()); | 403 sb.write(exp.value.unparse()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 void visitConditional(ConditionalConstantExpression exp, [_]) { | 459 void visitConditional(ConditionalConstantExpression exp, [_]) { |
| 461 write(exp, exp.condition, leftAssociative: false); | 460 write(exp, exp.condition, leftAssociative: false); |
| 462 sb.write(' ? '); | 461 sb.write(' ? '); |
| 463 write(exp, exp.trueExp); | 462 write(exp, exp.trueExp); |
| 464 sb.write(' : '); | 463 sb.write(' : '); |
| 465 write(exp, exp.falseExp); | 464 write(exp, exp.falseExp); |
| 466 } | 465 } |
| 467 | 466 |
| 468 String toString() => sb.toString(); | 467 String toString() => sb.toString(); |
| 469 } | 468 } |
| OLD | NEW |