| 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 import "dart:uri"; | 5 import "dart:uri"; |
| 6 import "../../../lib/compiler/implementation/elements/elements.dart"; | 6 import "../../../lib/compiler/implementation/elements/elements.dart"; |
| 7 import '../../../lib/compiler/implementation/scanner/scannerlib.dart'; | 7 import '../../../lib/compiler/implementation/scanner/scannerlib.dart'; |
| 8 import '../../../lib/compiler/implementation/source_file.dart'; | 8 import '../../../lib/compiler/implementation/source_file.dart'; |
| 9 import '../../../lib/compiler/implementation/types/types.dart'; | 9 import '../../../lib/compiler/implementation/types/types.dart'; |
| 10 import '../../../lib/compiler/implementation/tree/tree.dart'; | 10 import '../../../lib/compiler/implementation/tree/tree.dart'; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 class AnalysisResult { | 41 class AnalysisResult { |
| 42 MockCompiler compiler; | 42 MockCompiler compiler; |
| 43 ConcreteTypesInferrer inferrer; | 43 ConcreteTypesInferrer inferrer; |
| 44 Node ast; | 44 Node ast; |
| 45 | 45 |
| 46 BaseType int; | 46 BaseType int; |
| 47 BaseType double; | 47 BaseType double; |
| 48 BaseType num; |
| 48 BaseType bool; | 49 BaseType bool; |
| 49 BaseType string; | 50 BaseType string; |
| 50 BaseType list; | 51 BaseType list; |
| 51 BaseType map; | 52 BaseType map; |
| 52 BaseType nullType; | 53 BaseType nullType; |
| 53 | 54 |
| 54 AnalysisResult(MockCompiler compiler) : this.compiler = compiler { | 55 AnalysisResult(MockCompiler compiler) : this.compiler = compiler { |
| 55 inferrer = compiler.typesTask.concreteTypesInferrer; | 56 inferrer = compiler.typesTask.concreteTypesInferrer; |
| 56 int = inferrer.baseTypes.intBaseType; | 57 int = inferrer.baseTypes.intBaseType; |
| 57 double = inferrer.baseTypes.doubleBaseType; | 58 double = inferrer.baseTypes.doubleBaseType; |
| 59 num = inferrer.baseTypes.numBaseType; |
| 58 bool = inferrer.baseTypes.boolBaseType; | 60 bool = inferrer.baseTypes.boolBaseType; |
| 59 string = inferrer.baseTypes.stringBaseType; | 61 string = inferrer.baseTypes.stringBaseType; |
| 60 list = inferrer.baseTypes.listBaseType; | 62 list = inferrer.baseTypes.listBaseType; |
| 61 map = inferrer.baseTypes.mapBaseType; | 63 map = inferrer.baseTypes.mapBaseType; |
| 62 nullType = new NullBaseType(); | 64 nullType = new NullBaseType(); |
| 63 Element mainElement = compiler.mainApp.find(buildSourceString('main')); | 65 Element mainElement = compiler.mainApp.find(buildSourceString('main')); |
| 64 ast = mainElement.parseNode(compiler); | 66 ast = mainElement.parseNode(compiler); |
| 65 } | 67 } |
| 66 | 68 |
| 67 BaseType base(String className) { | 69 BaseType base(String className) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 * made of [baseTypes]. | 115 * made of [baseTypes]. |
| 114 */ | 116 */ |
| 115 void checkFieldHasType(String className, String fieldName, | 117 void checkFieldHasType(String className, String fieldName, |
| 116 List<BaseType> baseTypes) { | 118 List<BaseType> baseTypes) { |
| 117 return Expect.equals( | 119 return Expect.equals( |
| 118 concreteFrom(baseTypes), | 120 concreteFrom(baseTypes), |
| 119 inferrer.inferredFieldTypes[findField(className, fieldName)]); | 121 inferrer.inferredFieldTypes[findField(className, fieldName)]); |
| 120 } | 122 } |
| 121 } | 123 } |
| 122 | 124 |
| 125 const String CORELIB = r''' |
| 126 print(var obj) {} |
| 127 abstract class num { operator +(x); operator *(x); operator -(x); } |
| 128 abstract class int extends num { } |
| 129 abstract class double extends num { } |
| 130 class bool {} |
| 131 class String {} |
| 132 class Object {} |
| 133 class Function {} |
| 134 abstract class List {} |
| 135 abstract class Map {} |
| 136 class Closure {} |
| 137 class Null {} |
| 138 class Dynamic_ {} |
| 139 bool identical(Object a, Object b) {}'''; |
| 140 |
| 123 AnalysisResult analyze(String code) { | 141 AnalysisResult analyze(String code) { |
| 124 Uri uri = new Uri.fromComponents(scheme: 'source'); | 142 Uri uri = new Uri.fromComponents(scheme: 'source'); |
| 125 MockCompiler compiler = new MockCompiler(enableConcreteTypeInference: true); | 143 MockCompiler compiler = new MockCompiler(coreSource: CORELIB, |
| 144 enableConcreteTypeInference: true); |
| 126 compiler.sourceFiles[uri.toString()] = new SourceFile(uri.toString(), code); | 145 compiler.sourceFiles[uri.toString()] = new SourceFile(uri.toString(), code); |
| 127 compiler.runCompiler(uri); | 146 compiler.runCompiler(uri); |
| 128 return new AnalysisResult(compiler); | 147 return new AnalysisResult(compiler); |
| 129 } | 148 } |
| 130 | 149 |
| 131 testLiterals() { | 150 testLiterals() { |
| 132 final String source = r""" | 151 final String source = r""" |
| 133 main() { | 152 main() { |
| 134 var v1 = 42; | 153 var v1 = 42; |
| 135 var v2 = 42.0; | 154 var v2 = 42.0; |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 var x = f(); | 460 var x = f(); |
| 442 var y = g(); | 461 var y = g(); |
| 443 x; y; | 462 x; y; |
| 444 } | 463 } |
| 445 """; | 464 """; |
| 446 AnalysisResult result = analyze(source); | 465 AnalysisResult result = analyze(source); |
| 447 result.checkNodeHasType('x', [result.int, result.nullType]); | 466 result.checkNodeHasType('x', [result.int, result.nullType]); |
| 448 result.checkNodeHasType('y', [result.nullType]); | 467 result.checkNodeHasType('y', [result.nullType]); |
| 449 } | 468 } |
| 450 | 469 |
| 470 testArithmeticOperators() { |
| 471 String source(op) { |
| 472 return """ |
| 473 main() { |
| 474 var a = 1 $op 2; |
| 475 var b = 1 $op 2.0; |
| 476 var c = 1.0 $op 2; |
| 477 var d = 1.0 $op 2.0; |
| 478 var e = (1 $op 2.0) $op 1; |
| 479 var f = 1 $op (1 $op 2.0); |
| 480 var g = (1 $op 2.0) $op 1.0; |
| 481 var h = 1.0 $op (1 $op 2); |
| 482 var i = (1 $op 2) $op 1; |
| 483 var j = 1 $op (1 $op 2); |
| 484 var k = (1.0 $op 2.0) $op 1.0; |
| 485 var l = 1.0 $op (1.0 $op 2.0); |
| 486 a; b; c; d; e; f; g; h; i; j; k; l; |
| 487 }"""; |
| 488 } |
| 489 for (String op in ['+', '*', '-']) { |
| 490 AnalysisResult result = analyze(source(op)); |
| 491 result.checkNodeHasType('a', [result.int]); |
| 492 result.checkNodeHasType('b', [result.num]); |
| 493 result.checkNodeHasType('c', [result.num]); |
| 494 result.checkNodeHasType('d', [result.double]); |
| 495 result.checkNodeHasType('e', [result.num]); |
| 496 result.checkNodeHasType('f', [result.num]); |
| 497 result.checkNodeHasType('g', [result.num]); |
| 498 result.checkNodeHasType('h', [result.num]); |
| 499 result.checkNodeHasType('i', [result.int]); |
| 500 result.checkNodeHasType('j', [result.int]); |
| 501 result.checkNodeHasType('k', [result.double]); |
| 502 result.checkNodeHasType('l', [result.double]); |
| 503 } |
| 504 } |
| 505 |
| 506 testOperators() { |
| 507 final String source = r""" |
| 508 class A { |
| 509 operator <(x) => 42; |
| 510 operator <<(x) => "a"; |
| 511 } |
| 512 main() { |
| 513 var x = new A() < "foo"; |
| 514 var y = new A() << "foo"; |
| 515 x; y; |
| 516 } |
| 517 """; |
| 518 AnalysisResult result = analyze(source); |
| 519 result.checkNodeHasType('x', [result.int]); |
| 520 result.checkNodeHasType('y', [result.string]); |
| 521 } |
| 522 |
| 451 void main() { | 523 void main() { |
| 452 testLiterals(); | 524 testLiterals(); |
| 453 testRedefinition(); | 525 testRedefinition(); |
| 454 testIfThenElse(); | 526 testIfThenElse(); |
| 455 testTernaryIf(); | 527 testTernaryIf(); |
| 456 testWhile(); | 528 testWhile(); |
| 457 testFor1(); | 529 testFor1(); |
| 458 testFor2(); | 530 testFor2(); |
| 459 testNonRecusiveFunction(); | 531 testNonRecusiveFunction(); |
| 460 testRecusiveFunction(); | 532 testRecusiveFunction(); |
| 461 testMutuallyRecusiveFunction(); | 533 testMutuallyRecusiveFunction(); |
| 462 testSendToThis1(); | 534 testSendToThis1(); |
| 463 testSendToThis2(); | 535 testSendToThis2(); |
| 464 testConstructor(); | 536 testConstructor(); |
| 465 testGetters(); | 537 testGetters(); |
| 466 testSetters(); | 538 testSetters(); |
| 467 testNamedParameters(); | 539 testNamedParameters(); |
| 468 testListLiterals(); | 540 testListLiterals(); |
| 469 testMapLiterals(); | 541 testMapLiterals(); |
| 470 testReturn(); | 542 testReturn(); |
| 471 // testNoReturn(); // right now we infer the empty type instead of null | 543 // testNoReturn(); // right now we infer the empty type instead of null |
| 544 testArithmeticOperators(); |
| 545 testOperators(); |
| 472 } | 546 } |
| OLD | NEW |