| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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("../../../leg/leg.dart"); | 5 #import("../../../leg/leg.dart"); |
| 6 #import("../../../leg/elements/elements.dart"); | 6 #import("../../../leg/elements/elements.dart"); |
| 7 #import("../../../leg/tree/tree.dart"); | 7 #import("../../../leg/tree/tree.dart"); |
| 8 #import("../../../leg/util/util.dart"); | 8 #import("../../../leg/util/util.dart"); |
| 9 #import("mock_compiler.dart"); | 9 #import("mock_compiler.dart"); |
| 10 #import("parser_helper.dart"); | 10 #import("parser_helper.dart"); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 visitor.visit(function.body); | 106 visitor.visit(function.body); |
| 107 Map mapping = visitor.mapping.map; | 107 Map mapping = visitor.mapping.map; |
| 108 List<Element> values = mapping.getValues(); | 108 List<Element> values = mapping.getValues(); |
| 109 Expect.equals(0, mapping.length); | 109 Expect.equals(0, mapping.length); |
| 110 Expect.equals(0, compiler.warnings.length); | 110 Expect.equals(0, compiler.warnings.length); |
| 111 | 111 |
| 112 compiler = new MockCompiler(); | 112 compiler = new MockCompiler(); |
| 113 compiler.resolveStatement("main() { return this; }"); | 113 compiler.resolveStatement("main() { return this; }"); |
| 114 Expect.equals(0, compiler.warnings.length); | 114 Expect.equals(0, compiler.warnings.length); |
| 115 Expect.equals(1, compiler.errors.length); | 115 Expect.equals(1, compiler.errors.length); |
| 116 Expect.equals(MessageKind.NO_THIS_IN_STATIC, | 116 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, |
| 117 compiler.errors[0].message.kind); | 117 compiler.errors[0].message.kind); |
| 118 | 118 |
| 119 compiler = new MockCompiler(); | 119 compiler = new MockCompiler(); |
| 120 universe = compiler.universe; | 120 universe = compiler.universe; |
| 121 compiler.parseScript("class Foo { static foo() { return this; } }"); | 121 compiler.parseScript("class Foo { static foo() { return this; } }"); |
| 122 compiler.resolveStatement("Foo foo;"); | 122 compiler.resolveStatement("Foo foo;"); |
| 123 fooElement = universe.find(buildSourceString("Foo")); | 123 fooElement = universe.find(buildSourceString("Foo")); |
| 124 funElement = | 124 funElement = |
| 125 fooElement.lookupLocalMember(buildSourceString("foo")); | 125 fooElement.lookupLocalMember(buildSourceString("foo")); |
| 126 visitor = new FullResolverVisitor(compiler, funElement); | 126 visitor = new FullResolverVisitor(compiler, funElement); |
| 127 function = funElement.parseNode(compiler, compiler); | 127 function = funElement.parseNode(compiler, compiler); |
| 128 visitor.visit(function.body); | 128 visitor.visit(function.body); |
| 129 Expect.equals(0, compiler.warnings.length); | 129 Expect.equals(0, compiler.warnings.length); |
| 130 Expect.equals(1, compiler.errors.length); | 130 Expect.equals(1, compiler.errors.length); |
| 131 Expect.equals(MessageKind.NO_THIS_IN_STATIC, | 131 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, |
| 132 compiler.errors[0].message.kind); | 132 compiler.errors[0].message.kind); |
| 133 } | 133 } |
| 134 | 134 |
| 135 testLocalsOne() { | 135 testLocalsOne() { |
| 136 testLocals([["foo", false]]); | 136 testLocals([["foo", false]]); |
| 137 testLocals([["foo", false], ["bar", false]]); | 137 testLocals([["foo", false], ["bar", false]]); |
| 138 testLocals([["foo", false], ["bar", false], ["foobar", false]]); | 138 testLocals([["foo", false], ["bar", false], ["foobar", false]]); |
| 139 | 139 |
| 140 testLocals([["foo", true]]); | 140 testLocals([["foo", true]]); |
| 141 testLocals([["foo", false], ["bar", true]]); | 141 testLocals([["foo", false], ["bar", true]]); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 Expect.equals(ElementKind.GENERATIVE_CONSTRUCTOR, element.kind); | 422 Expect.equals(ElementKind.GENERATIVE_CONSTRUCTOR, element.kind); |
| 423 Expect.isTrue(element is SynthesizedConstructorElement); | 423 Expect.isTrue(element is SynthesizedConstructorElement); |
| 424 } | 424 } |
| 425 | 425 |
| 426 testTopLevelFields() { | 426 testTopLevelFields() { |
| 427 MockCompiler compiler = new MockCompiler(); | 427 MockCompiler compiler = new MockCompiler(); |
| 428 compiler.parseScript("int a;"); | 428 compiler.parseScript("int a;"); |
| 429 VariableElement element = compiler.universe.find(buildSourceString("a")); | 429 VariableElement element = compiler.universe.find(buildSourceString("a")); |
| 430 Expect.equals(ElementKind.FIELD, element.kind); | 430 Expect.equals(ElementKind.FIELD, element.kind); |
| 431 VariableDefinitions node = element.variables.parseNode(compiler, compiler); | 431 VariableDefinitions node = element.variables.parseNode(compiler, compiler); |
| 432 Expect.equals(node.type.typeName.asIdentifier().source.stringValue, 'int'); | 432 Identifier typeName = node.type.typeName; |
| 433 Expect.equals(typeName.source.stringValue, 'int'); |
| 433 | 434 |
| 434 compiler.parseScript("var b, c;"); | 435 compiler.parseScript("var b, c;"); |
| 435 VariableElement bElement = compiler.universe.find(buildSourceString("b")); | 436 VariableElement bElement = compiler.universe.find(buildSourceString("b")); |
| 436 VariableElement cElement = compiler.universe.find(buildSourceString("c")); | 437 VariableElement cElement = compiler.universe.find(buildSourceString("c")); |
| 437 Expect.equals(ElementKind.FIELD, bElement.kind); | 438 Expect.equals(ElementKind.FIELD, bElement.kind); |
| 438 Expect.equals(ElementKind.FIELD, cElement.kind); | 439 Expect.equals(ElementKind.FIELD, cElement.kind); |
| 439 Expect.isTrue(bElement != cElement); | 440 Expect.isTrue(bElement != cElement); |
| 440 | 441 |
| 441 VariableDefinitions bNode = bElement.variables.parseNode(compiler, compiler); | 442 VariableDefinitions bNode = bElement.variables.parseNode(compiler, compiler); |
| 442 VariableDefinitions cNode = cElement.variables.parseNode(compiler, compiler); | 443 VariableDefinitions cNode = cElement.variables.parseNode(compiler, compiler); |
| 443 Expect.equals(bNode, cNode); | 444 Expect.equals(bNode, cNode); |
| 444 Expect.isNull(bNode.type); | 445 Expect.isNull(bNode.type); |
| 445 Expect.isTrue(bNode.modifiers.isVar()); | 446 Expect.isTrue(bNode.modifiers.isVar()); |
| 446 } | 447 } |
| 447 | 448 |
| 448 resolveConstructor(String script, String statement, String className, | 449 resolveConstructor(String script, String statement, String className, |
| 449 String constructor, int expectedElementCount, | 450 String constructor, int expectedElementCount, |
| 450 [List expectedWarnings = const [], | 451 [List expectedWarnings = const [], |
| 451 List expectedErrors = const []]) { | 452 List expectedErrors = const [], |
| 452 MockCompiler compiler = new MockCompiler(); | 453 String corelib = DEFAULT_CORELIB]) { |
| 454 MockCompiler compiler = new MockCompiler(corelib); |
| 453 compiler.parseScript(script); | 455 compiler.parseScript(script); |
| 454 compiler.resolveStatement(statement); | 456 compiler.resolveStatement(statement); |
| 455 ClassElement classElement = | 457 ClassElement classElement = |
| 456 compiler.universe.find(buildSourceString(className)); | 458 compiler.universe.find(buildSourceString(className)); |
| 457 Element element = | 459 Element element = |
| 458 classElement.lookupConstructor(buildSourceString(constructor)); | 460 classElement.lookupConstructor(buildSourceString(constructor)); |
| 459 FunctionExpression tree = element.parseNode(compiler, compiler); | 461 FunctionExpression tree = element.parseNode(compiler, compiler); |
| 460 ResolverVisitor visitor = new FullResolverVisitor(compiler, element); | 462 ResolverVisitor visitor = new FullResolverVisitor(compiler, element); |
| 461 compiler.resolver.resolveInitializers(element, tree, visitor); | 463 new InitializerResolver(visitor, element).resolveInitializers(tree); |
| 464 visitor.visit(tree.body); |
| 462 Expect.equals(expectedElementCount, visitor.mapping.map.length); | 465 Expect.equals(expectedElementCount, visitor.mapping.map.length); |
| 463 | 466 |
| 464 compareWarningKinds(script, expectedWarnings, compiler.warnings); | 467 compareWarningKinds(script, expectedWarnings, compiler.warnings); |
| 465 compareWarningKinds(script, expectedErrors, compiler.errors); | 468 compareWarningKinds(script, expectedErrors, compiler.errors); |
| 466 } | 469 } |
| 467 | 470 |
| 468 testInitializers() { | 471 testInitializers() { |
| 469 String script; | 472 String script; |
| 470 script = """class A { | 473 script = """class A { |
| 471 int foo; int bar; | 474 int foo; int bar; |
| 472 A() : this.foo = 1, bar = 2; | 475 A() : this.foo = 1, bar = 2; |
| 473 }"""; | 476 }"""; |
| 474 resolveConstructor(script, "A a = new A();", "A", "A", 2); | 477 resolveConstructor(script, "A a = new A();", "A", "A", 2); |
| 475 | 478 |
| 476 script = """class A { | 479 script = """class A { |
| 477 int foo; A a; | 480 int foo; A a; |
| 478 A() : a.foo = 1; | 481 A() : a.foo = 1; |
| 479 }"""; | 482 }"""; |
| 480 resolveConstructor(script, "A a = new A();", "A", "A", 1, | 483 resolveConstructor(script, "A a = new A();", "A", "A", 0, |
| 481 [], [MessageKind.INVALID_RECEIVER_IN_INITIALIZER]); | 484 [], [MessageKind.INVALID_RECEIVER_IN_INITIALIZER]); |
| 482 | 485 |
| 483 script = """class A { | 486 script = """class A { |
| 484 int foo; | 487 int foo; |
| 485 A() : this.foo = 1, this.foo = 2; | 488 A() : this.foo = 1, this.foo = 2; |
| 486 }"""; | 489 }"""; |
| 487 resolveConstructor(script, "A a = new A();", "A", "A", 2, | 490 resolveConstructor(script, "A a = new A();", "A", "A", 2, |
| 488 [MessageKind.ALREADY_INITIALIZED], | 491 [MessageKind.ALREADY_INITIALIZED], |
| 489 [MessageKind.DUPLICATE_INITIALIZER]); | 492 [MessageKind.DUPLICATE_INITIALIZER]); |
| 490 | 493 |
| 491 script = """class A { | 494 script = """class A { |
| 492 A() : this.foo = 1; | 495 A() : this.foo = 1; |
| 493 }"""; | 496 }"""; |
| 494 resolveConstructor(script, "A a = new A();", "A", "A", 0, | 497 resolveConstructor(script, "A a = new A();", "A", "A", 0, |
| 495 [], [MessageKind.CANNOT_RESOLVE]); | 498 [], [MessageKind.CANNOT_RESOLVE]); |
| 496 | 499 |
| 497 script = """class A { | 500 script = """class A { |
| 498 int foo; | 501 int foo; |
| 499 int bar; | 502 int bar; |
| 500 A() : this.foo = bar; | 503 A() : this.foo = bar; |
| 501 }"""; | 504 }"""; |
| 502 resolveConstructor(script, "A a = new A();", "A", "A", 2, | 505 resolveConstructor(script, "A a = new A();", "A", "A", 2, |
| 503 [], [MessageKind.NOT_STATIC]); | 506 [], [MessageKind.NO_INSTANCE_AVAILABLE]); |
| 507 |
| 508 script = """class A { |
| 509 int foo() => 42; |
| 510 A() : foo(); |
| 511 }"""; |
| 512 resolveConstructor(script, "A a = new A();", "A", "A", 0, |
| 513 [], [MessageKind.CONSTRUCTOR_CALL_EXPECTED]); |
| 514 |
| 515 script = """class A { |
| 516 int i; |
| 517 A.a() : this.b(0); |
| 518 A.b(int i); |
| 519 }"""; |
| 520 resolveConstructor(script, "A a = new A.a();", "A", "A.a", 1, |
| 521 [], []); |
| 522 |
| 523 script = """class A { |
| 524 int i; |
| 525 A.a() : i = 42, this(0); |
| 526 A(int i); |
| 527 }"""; |
| 528 resolveConstructor(script, "A a = new A.a();", "A", "A.a", 2, |
| 529 [], [MessageKind.REDIRECTING_CTOR_HAS_INITIALIZER]); |
| 530 |
| 531 script = """class A { |
| 532 int i; |
| 533 A(i); |
| 534 } |
| 535 class B extends A { |
| 536 B() : super(0); |
| 537 }"""; |
| 538 resolveConstructor(script, "B a = new B();", "B", "B", 1, |
| 539 [], []); |
| 540 |
| 541 script = """class A { |
| 542 int i; |
| 543 A(i); |
| 544 } |
| 545 class B extends A { |
| 546 B() : super(0), super(1); |
| 547 }"""; |
| 548 resolveConstructor(script, "B b = new B();", "B", "B", 2, |
| 549 [], [MessageKind.DUPLICATE_SUPER_INITIALIZER]); |
| 550 |
| 551 script = "class Object { Object() : super(); }"; |
| 552 resolveConstructor(script, "Object o = new Object();", "Object", "Object", 1, |
| 553 [], [MessageKind.SUPER_INITIALIZER_IN_OBJECT], |
| 554 corelib: ""); |
| 504 } | 555 } |
| 505 | 556 |
| 506 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; | 557 length(Link link) => link.isEmpty() ? 0 : length(link.tail) + 1; |
| 507 | 558 |
| 508 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); | 559 at(Link link, int index) => (index == 0) ? link.head : at(link.tail, index - 1); |
| OLD | NEW |