OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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.compile_time_constant_evaluator; | 5 library dart2js.compile_time_constant_evaluator; |
6 | 6 |
7 import 'common/resolution.dart' show Resolution; | 7 import 'common/resolution.dart' show Resolution; |
8 import 'common/tasks.dart' show CompilerTask, Measurer; | 8 import 'common/tasks.dart' show CompilerTask, Measurer; |
9 import 'common.dart'; | 9 import 'common.dart'; |
10 import 'compiler.dart' show Compiler; | 10 import 'compiler.dart' show Compiler; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 /** The set of variable elements that are in the process of being computed. */ | 154 /** The set of variable elements that are in the process of being computed. */ |
155 final Set<VariableElement> pendingVariables = new Set<VariableElement>(); | 155 final Set<VariableElement> pendingVariables = new Set<VariableElement>(); |
156 | 156 |
157 final Map<ConstantExpression, ConstantValue> constantValueMap = | 157 final Map<ConstantExpression, ConstantValue> constantValueMap = |
158 <ConstantExpression, ConstantValue>{}; | 158 <ConstantExpression, ConstantValue>{}; |
159 | 159 |
160 ConstantCompilerBase(this.compiler, this.constantSystem); | 160 ConstantCompilerBase(this.compiler, this.constantSystem); |
161 | 161 |
162 DiagnosticReporter get reporter => compiler.reporter; | 162 DiagnosticReporter get reporter => compiler.reporter; |
163 | 163 |
164 CommonElements get commonElements => compiler.commonElements; | 164 CommonElements get commonElements => compiler.resolution.commonElements; |
165 | 165 |
166 @override | 166 @override |
167 @deprecated | 167 @deprecated |
168 ConstantValue getConstantValueForVariable(VariableElement element) { | 168 ConstantValue getConstantValueForVariable(VariableElement element) { |
169 ConstantExpression constant = initialVariableValues[element.declaration]; | 169 ConstantExpression constant = initialVariableValues[element.declaration]; |
170 // TODO(johnniwinther): Support eager evaluation of the constant. | 170 // TODO(johnniwinther): Support eager evaluation of the constant. |
171 return constant != null ? getConstantValue(constant) : null; | 171 return constant != null ? getConstantValue(constant) : null; |
172 } | 172 } |
173 | 173 |
174 ConstantExpression compileConstant(VariableElement element) { | 174 ConstantExpression compileConstant(VariableElement element) { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 final Compiler compiler; | 399 final Compiler compiler; |
400 | 400 |
401 Element get context => elements.analyzedElement; | 401 Element get context => elements.analyzedElement; |
402 | 402 |
403 CompileTimeConstantEvaluator(this.handler, this.elements, this.compiler, | 403 CompileTimeConstantEvaluator(this.handler, this.elements, this.compiler, |
404 {bool isConst: false}) | 404 {bool isConst: false}) |
405 : this.isEvaluatingConstant = isConst; | 405 : this.isEvaluatingConstant = isConst; |
406 | 406 |
407 ConstantSystem get constantSystem => handler.constantSystem; | 407 ConstantSystem get constantSystem => handler.constantSystem; |
408 Resolution get resolution => compiler.resolution; | 408 Resolution get resolution => compiler.resolution; |
409 CommonElements get commonElements => compiler.commonElements; | 409 CommonElements get commonElements => resolution.commonElements; |
410 DiagnosticReporter get reporter => compiler.reporter; | 410 DiagnosticReporter get reporter => compiler.reporter; |
411 | 411 |
412 AstConstant evaluate(Node node) { | 412 AstConstant evaluate(Node node) { |
413 // TODO(johnniwinther): should there be a visitErrorNode? | 413 // TODO(johnniwinther): should there be a visitErrorNode? |
414 if (node is ErrorNode) return new ErroneousAstConstant(context, node); | 414 if (node is ErrorNode) return new ErroneousAstConstant(context, node); |
415 AstConstant result = node.accept(this); | 415 AstConstant result = node.accept(this); |
416 assert(!isEvaluatingConstant || result != null, | 416 assert(!isEvaluatingConstant || result != null, |
417 failedAt(node, "No AstConstant computed for the node.")); | 417 failedAt(node, "No AstConstant computed for the node.")); |
418 return result; | 418 return result; |
419 } | 419 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
506 keyExpressions.add(key.expression); | 506 keyExpressions.add(key.expression); |
507 valueExpressions.add(value.expression); | 507 valueExpressions.add(value.expression); |
508 map[key.value] = value.value; | 508 map[key.value] = value.value; |
509 } | 509 } |
510 ResolutionInterfaceType type = elements.getType(node); | 510 ResolutionInterfaceType type = elements.getType(node); |
511 return new AstConstant( | 511 return new AstConstant( |
512 context, | 512 context, |
513 node, | 513 node, |
514 new MapConstantExpression(type, keyExpressions, valueExpressions), | 514 new MapConstantExpression(type, keyExpressions, valueExpressions), |
515 constantSystem.createMap( | 515 constantSystem.createMap( |
516 compiler.commonElements, type, keyValues, map.values.toList())); | 516 resolution.commonElements, type, keyValues, map.values.toList())); |
517 } | 517 } |
518 | 518 |
519 AstConstant visitLiteralNull(LiteralNull node) { | 519 AstConstant visitLiteralNull(LiteralNull node) { |
520 return new AstConstant(context, node, new NullConstantExpression(), | 520 return new AstConstant(context, node, new NullConstantExpression(), |
521 constantSystem.createNull()); | 521 constantSystem.createNull()); |
522 } | 522 } |
523 | 523 |
524 AstConstant visitLiteralString(LiteralString node) { | 524 AstConstant visitLiteralString(LiteralString node) { |
525 String text = node.dartString.slowToString(); | 525 String text = node.dartString.slowToString(); |
526 return new AstConstant(context, node, new StringConstantExpression(text), | 526 return new AstConstant(context, node, new StringConstantExpression(text), |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 } | 585 } |
586 | 586 |
587 AstConstant visitLiteralSymbol(LiteralSymbol node) { | 587 AstConstant visitLiteralSymbol(LiteralSymbol node) { |
588 ResolutionInterfaceType type = commonElements.symbolImplementationType; | 588 ResolutionInterfaceType type = commonElements.symbolImplementationType; |
589 String text = node.slowNameString; | 589 String text = node.slowNameString; |
590 List<AstConstant> arguments = <AstConstant>[ | 590 List<AstConstant> arguments = <AstConstant>[ |
591 new AstConstant(context, node, new StringConstantExpression(text), | 591 new AstConstant(context, node, new StringConstantExpression(text), |
592 constantSystem.createString(text)) | 592 constantSystem.createString(text)) |
593 ]; | 593 ]; |
594 ConstructorElement constructor = | 594 ConstructorElement constructor = |
595 compiler.commonElements.symbolConstructorTarget; | 595 resolution.commonElements.symbolConstructorTarget; |
596 AstConstant constant = createConstructorInvocation( | 596 AstConstant constant = createConstructorInvocation( |
597 node, type, constructor, CallStructure.ONE_ARG, | 597 node, type, constructor, CallStructure.ONE_ARG, |
598 normalizedArguments: arguments); | 598 normalizedArguments: arguments); |
599 return new AstConstant( | 599 return new AstConstant( |
600 context, node, new SymbolConstantExpression(text), constant.value); | 600 context, node, new SymbolConstantExpression(text), constant.value); |
601 } | 601 } |
602 | 602 |
603 ConstantValue makeTypeConstant(ResolutionDartType elementType) { | 603 ConstantValue makeTypeConstant(ResolutionDartType elementType) { |
604 return constantSystem.createType(compiler.commonElements, elementType); | 604 return constantSystem.createType(resolution.commonElements, elementType); |
605 } | 605 } |
606 | 606 |
607 /// Returns true if the prefix of the send resolves to a deferred import | 607 /// Returns true if the prefix of the send resolves to a deferred import |
608 /// prefix. | 608 /// prefix. |
609 bool isDeferredUse(Send send) { | 609 bool isDeferredUse(Send send) { |
610 if (send == null) return false; | 610 if (send == null) return false; |
611 return compiler.deferredLoadTask.deferredPrefixElement(send, elements) != | 611 return compiler.deferredLoadTask.deferredPrefixElement(send, elements) != |
612 null; | 612 null; |
613 } | 613 } |
614 | 614 |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
700 result = new AstConstant( | 700 result = new AstConstant( |
701 context, | 701 context, |
702 send, | 702 send, |
703 new DeferredConstantExpression(result.expression, prefix), | 703 new DeferredConstantExpression(result.expression, prefix), |
704 new DeferredConstantValue(result.value, prefix)); | 704 new DeferredConstantValue(result.value, prefix)); |
705 compiler.deferredLoadTask | 705 compiler.deferredLoadTask |
706 .registerConstantDeferredUse(result.value, prefix); | 706 .registerConstantDeferredUse(result.value, prefix); |
707 } | 707 } |
708 return result; | 708 return result; |
709 } else if (send.isCall) { | 709 } else if (send.isCall) { |
710 if (element == compiler.commonElements.identicalFunction && | 710 if (element == resolution.commonElements.identicalFunction && |
711 send.argumentCount() == 2) { | 711 send.argumentCount() == 2) { |
712 AstConstant left = evaluate(send.argumentsNode.nodes.head); | 712 AstConstant left = evaluate(send.argumentsNode.nodes.head); |
713 AstConstant right = evaluate(send.argumentsNode.nodes.tail.head); | 713 AstConstant right = evaluate(send.argumentsNode.nodes.tail.head); |
714 if (left == null || right == null) { | 714 if (left == null || right == null) { |
715 return null; | 715 return null; |
716 } | 716 } |
717 ConstantValue result = | 717 ConstantValue result = |
718 constantSystem.identity.fold(left.value, right.value); | 718 constantSystem.identity.fold(left.value, right.value); |
719 if (result != null) { | 719 if (result != null) { |
720 return new AstConstant( | 720 return new AstConstant( |
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1443 new ErroneousConstantExpression(), | 1443 new ErroneousConstantExpression(), |
1444 new NullConstantValue()); | 1444 new NullConstantValue()); |
1445 } | 1445 } |
1446 | 1446 |
1447 class _CompilerEnvironment implements EvaluationEnvironment { | 1447 class _CompilerEnvironment implements EvaluationEnvironment { |
1448 final Compiler _compiler; | 1448 final Compiler _compiler; |
1449 | 1449 |
1450 _CompilerEnvironment(this._compiler); | 1450 _CompilerEnvironment(this._compiler); |
1451 | 1451 |
1452 @override | 1452 @override |
1453 CommonElements get commonElements => _compiler.commonElements; | 1453 CommonElements get commonElements => _compiler.resolution.commonElements; |
1454 | 1454 |
1455 @override | 1455 @override |
1456 String readFromEnvironment(String name) { | 1456 String readFromEnvironment(String name) { |
1457 return _compiler.fromEnvironment(name); | 1457 return _compiler.fromEnvironment(name); |
1458 } | 1458 } |
1459 | 1459 |
1460 @override | 1460 @override |
1461 ResolutionInterfaceType substByContext( | 1461 ResolutionInterfaceType substByContext( |
1462 ResolutionInterfaceType base, ResolutionInterfaceType target) { | 1462 ResolutionInterfaceType base, ResolutionInterfaceType target) { |
1463 return base.substByContext(target); | 1463 return base.substByContext(target); |
1464 } | 1464 } |
1465 | 1465 |
1466 @override | 1466 @override |
1467 ConstantConstructor getConstructorConstant(ConstructorElement constructor) { | 1467 ConstantConstructor getConstructorConstant(ConstructorElement constructor) { |
1468 return constructor.constantConstructor; | 1468 return constructor.constantConstructor; |
1469 } | 1469 } |
1470 | 1470 |
1471 @override | 1471 @override |
1472 ConstantExpression getFieldConstant(FieldElement field) { | 1472 ConstantExpression getFieldConstant(FieldElement field) { |
1473 return field.constant; | 1473 return field.constant; |
1474 } | 1474 } |
1475 | 1475 |
1476 @override | 1476 @override |
1477 ConstantExpression getLocalConstant(LocalVariableElement local) { | 1477 ConstantExpression getLocalConstant(LocalVariableElement local) { |
1478 return local.constant; | 1478 return local.constant; |
1479 } | 1479 } |
1480 } | 1480 } |
OLD | NEW |