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:async'; | 5 import 'dart:async'; |
6 import 'dart:collection'; | 6 import 'dart:collection'; |
7 | 7 |
8 import 'package:async_helper/async_helper.dart'; | 8 import 'package:async_helper/async_helper.dart'; |
9 import 'package:expect/expect.dart'; | 9 import 'package:expect/expect.dart'; |
10 import 'package:compiler/src/constants/expressions.dart'; | 10 import 'package:compiler/src/constants/expressions.dart'; |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 String script = """class A { foo() {} } | 215 String script = """class A { foo() {} } |
216 class B extends A { foo() => super.foo(); }"""; | 216 class B extends A { foo() => super.foo(); }"""; |
217 compiler.parseScript(script); | 217 compiler.parseScript(script); |
218 compiler.resolveStatement("B b;"); | 218 compiler.resolveStatement("B b;"); |
219 | 219 |
220 ClassElement classB = compiler.mainApp.find("B"); | 220 ClassElement classB = compiler.mainApp.find("B"); |
221 FunctionElement fooB = classB.lookupLocalMember("foo"); | 221 FunctionElement fooB = classB.lookupLocalMember("foo"); |
222 ClassElement classA = compiler.mainApp.find("A"); | 222 ClassElement classA = compiler.mainApp.find("A"); |
223 FunctionElement fooA = classA.lookupLocalMember("foo"); | 223 FunctionElement fooA = classA.lookupLocalMember("foo"); |
224 | 224 |
225 ResolverVisitor visitor = new ResolverVisitor( | 225 ResolverVisitor visitor = |
226 compiler, | 226 new ResolverVisitor(compiler, fooB, |
227 fooB, | 227 new ResolutionRegistry(compiler, |
228 new ResolutionRegistry(compiler, new CollectingTreeElements(fooB)), | 228 new CollectingTreeElements(fooB))); |
229 scope: new MockTypeVariablesScope(classB.buildScope())); | |
230 FunctionExpression node = | 229 FunctionExpression node = |
231 (fooB as FunctionElementX).parseNode(compiler.parsingContext); | 230 (fooB as FunctionElementX).parseNode(compiler.parsingContext); |
232 visitor.visit(node.body); | 231 visitor.visit(node.body); |
233 Map mapping = map(visitor); | 232 Map mapping = map(visitor); |
234 | 233 |
235 Send superCall = node.body.asReturn().expression; | 234 Send superCall = node.body.asReturn().expression; |
236 FunctionElement called = mapping[superCall]; | 235 FunctionElement called = mapping[superCall]; |
237 Expect.isNotNull(called); | 236 Expect.isNotNull(called); |
238 Expect.equals(fooA, called); | 237 Expect.equals(fooA, called); |
239 }); | 238 }); |
(...skipping 20 matching lines...) Expand all Loading... |
260 }); | 259 }); |
261 } | 260 } |
262 | 261 |
263 Future testThis() { | 262 Future testThis() { |
264 return Future.wait([ | 263 return Future.wait([ |
265 MockCompiler.create((MockCompiler compiler) { | 264 MockCompiler.create((MockCompiler compiler) { |
266 compiler.parseScript("class Foo { foo() { return this; } }"); | 265 compiler.parseScript("class Foo { foo() { return this; } }"); |
267 compiler.resolveStatement("Foo foo;"); | 266 compiler.resolveStatement("Foo foo;"); |
268 ClassElement fooElement = compiler.mainApp.find("Foo"); | 267 ClassElement fooElement = compiler.mainApp.find("Foo"); |
269 FunctionElement funElement = fooElement.lookupLocalMember("foo"); | 268 FunctionElement funElement = fooElement.lookupLocalMember("foo"); |
270 ResolverVisitor visitor = new ResolverVisitor( | 269 ResolverVisitor visitor = |
271 compiler, | 270 new ResolverVisitor(compiler, funElement, |
272 funElement, | 271 new ResolutionRegistry(compiler, |
273 new ResolutionRegistry( | 272 new CollectingTreeElements(funElement))); |
274 compiler, new CollectingTreeElements(funElement)), | |
275 scope: new MockTypeVariablesScope(fooElement.buildScope())); | |
276 FunctionExpression function = | 273 FunctionExpression function = |
277 (funElement as FunctionElementX).parseNode(compiler.parsingContext); | 274 (funElement as FunctionElementX).parseNode(compiler.parsingContext); |
278 visitor.visit(function.body); | 275 visitor.visit(function.body); |
279 Map mapping = map(visitor); | 276 Map mapping = map(visitor); |
280 List<Element> values = mapping.values.toList(); | 277 List<Element> values = mapping.values.toList(); |
281 DiagnosticCollector collector = compiler.diagnosticCollector; | 278 DiagnosticCollector collector = compiler.diagnosticCollector; |
282 Expect.equals(0, mapping.length); | 279 Expect.equals(0, mapping.length); |
283 Expect.equals(0, collector.warnings.length); | 280 Expect.equals(0, collector.warnings.length); |
284 }), | 281 }), |
285 MockCompiler.create((MockCompiler compiler) { | 282 MockCompiler.create((MockCompiler compiler) { |
286 compiler.resolveStatement("main() { return this; }"); | 283 compiler.resolveStatement("main() { return this; }"); |
287 DiagnosticCollector collector = compiler.diagnosticCollector; | 284 DiagnosticCollector collector = compiler.diagnosticCollector; |
288 Expect.equals(0, collector.warnings.length); | 285 Expect.equals(0, collector.warnings.length); |
289 Expect.equals(1, collector.errors.length); | 286 Expect.equals(1, collector.errors.length); |
290 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, | 287 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, |
291 collector.errors.first.message.kind); | 288 collector.errors.first.message.kind); |
292 }), | 289 }), |
293 MockCompiler.create((MockCompiler compiler) { | 290 MockCompiler.create((MockCompiler compiler) { |
294 compiler.parseScript("class Foo { static foo() { return this; } }"); | 291 compiler.parseScript("class Foo { static foo() { return this; } }"); |
295 compiler.resolveStatement("Foo foo;"); | 292 compiler.resolveStatement("Foo foo;"); |
296 ClassElement fooElement = compiler.mainApp.find("Foo"); | 293 ClassElement fooElement = compiler.mainApp.find("Foo"); |
297 FunctionElement funElement = fooElement.lookupLocalMember("foo"); | 294 FunctionElement funElement = fooElement.lookupLocalMember("foo"); |
298 ResolverVisitor visitor = new ResolverVisitor( | 295 ResolverVisitor visitor = new ResolverVisitor(compiler, funElement, |
299 compiler, | 296 new ResolutionRegistry(compiler, |
300 funElement, | 297 new CollectingTreeElements(funElement))); |
301 new ResolutionRegistry( | |
302 compiler, new CollectingTreeElements(funElement)), | |
303 scope: new MockTypeVariablesScope(fooElement.buildScope())); | |
304 FunctionExpression function = | 298 FunctionExpression function = |
305 (funElement as FunctionElementX).parseNode(compiler.parsingContext); | 299 (funElement as FunctionElementX).parseNode(compiler.parsingContext); |
306 visitor.visit(function.body); | 300 visitor.visit(function.body); |
307 DiagnosticCollector collector = compiler.diagnosticCollector; | 301 DiagnosticCollector collector = compiler.diagnosticCollector; |
308 Expect.equals(0, collector.warnings.length); | 302 Expect.equals(0, collector.warnings.length); |
309 Expect.equals(1, collector.errors.length); | 303 Expect.equals(1, collector.errors.length); |
310 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, | 304 Expect.equals(MessageKind.NO_INSTANCE_AVAILABLE, |
311 collector.errors.first.message.kind); | 305 collector.errors.first.message.kind); |
312 }), | 306 }), |
313 ]); | 307 ]); |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
580 new Message( | 574 new Message( |
581 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], | 575 MessageTemplate.TEMPLATES[MessageKind.CANNOT_RESOLVE_TYPE], |
582 {'typeName': 'bar'}, false), | 576 {'typeName': 'bar'}, false), |
583 collector.errors.first.message); | 577 collector.errors.first.message); |
584 collector.clear(); | 578 collector.clear(); |
585 | 579 |
586 // Add the abstract class to the world and make sure everything is setup | 580 // Add the abstract class to the world and make sure everything is setup |
587 // correctly. | 581 // correctly. |
588 compiler.parseScript("abstract class Bar {}"); | 582 compiler.parseScript("abstract class Bar {}"); |
589 | 583 |
590 ResolverVisitor visitor = new ResolverVisitor( | 584 ResolverVisitor visitor = |
591 compiler, | 585 new ResolverVisitor(compiler, null, |
592 null, | 586 new ResolutionRegistry(compiler, |
593 new ResolutionRegistry(compiler, new CollectingTreeElements(null))); | 587 new CollectingTreeElements(null))); |
594 compiler.resolveStatement("Foo bar;"); | 588 compiler.resolveStatement("Foo bar;"); |
595 | 589 |
596 ClassElement fooElement = compiler.mainApp.find('Foo'); | 590 ClassElement fooElement = compiler.mainApp.find('Foo'); |
597 ClassElement barElement = compiler.mainApp.find('Bar'); | 591 ClassElement barElement = compiler.mainApp.find('Bar'); |
598 | 592 |
599 Expect.equals(null, barElement.supertype); | 593 Expect.equals(null, barElement.supertype); |
600 Expect.isTrue(barElement.interfaces.isEmpty); | 594 Expect.isTrue(barElement.interfaces.isEmpty); |
601 | 595 |
602 Expect.equals(barElement.computeType(compiler.resolution), | 596 Expect.equals(barElement.computeType(compiler.resolution), |
603 fooElement.interfaces.head); | 597 fooElement.interfaces.head); |
(...skipping 14 matching lines...) Expand all Loading... |
618 ClassElement i2 = compiler.mainApp.find('I2'); | 612 ClassElement i2 = compiler.mainApp.find('I2'); |
619 | 613 |
620 Expect.equals(2, length(c.interfaces)); | 614 Expect.equals(2, length(c.interfaces)); |
621 Expect.equals(i1.computeType(compiler.resolution), at(c.interfaces, 0)); | 615 Expect.equals(i1.computeType(compiler.resolution), at(c.interfaces, 0)); |
622 Expect.equals(i2.computeType(compiler.resolution), at(c.interfaces, 1)); | 616 Expect.equals(i2.computeType(compiler.resolution), at(c.interfaces, 1)); |
623 }); | 617 }); |
624 } | 618 } |
625 | 619 |
626 Future testFunctionExpression() { | 620 Future testFunctionExpression() { |
627 return MockCompiler.create((MockCompiler compiler) { | 621 return MockCompiler.create((MockCompiler compiler) { |
| 622 ResolverVisitor visitor = compiler.resolverVisitor(); |
628 Map mapping = compiler.resolveStatement("int f() {}").map; | 623 Map mapping = compiler.resolveStatement("int f() {}").map; |
629 Expect.equals(2, mapping.length); | 624 Expect.equals(2, mapping.length); |
630 Element element; | 625 Element element; |
631 Node node; | 626 Node node; |
632 mapping.forEach((Node n, Element e) { | 627 mapping.forEach((Node n, Element e) { |
633 if (n is FunctionExpression) { | 628 if (n is FunctionExpression) { |
634 element = e; | 629 element = e; |
635 node = n; | 630 node = n; |
636 } | 631 } |
637 }); | 632 }); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
699 List expectedInfos: const [], | 694 List expectedInfos: const [], |
700 Map<String, String> corelib}) { | 695 Map<String, String> corelib}) { |
701 MockCompiler compiler = new MockCompiler.internal(coreSource: corelib); | 696 MockCompiler compiler = new MockCompiler.internal(coreSource: corelib); |
702 return compiler.init().then((_) { | 697 return compiler.init().then((_) { |
703 compiler.parseScript(script); | 698 compiler.parseScript(script); |
704 compiler.resolveStatement(statement); | 699 compiler.resolveStatement(statement); |
705 ClassElement classElement = compiler.mainApp.find(className); | 700 ClassElement classElement = compiler.mainApp.find(className); |
706 Element element; | 701 Element element; |
707 element = classElement.lookupConstructor(constructor); | 702 element = classElement.lookupConstructor(constructor); |
708 FunctionExpression tree = (element as FunctionElement).node; | 703 FunctionExpression tree = (element as FunctionElement).node; |
709 ResolverVisitor visitor = new ResolverVisitor( | 704 ResolverVisitor visitor = |
710 compiler, | 705 new ResolverVisitor(compiler, element, |
711 element, | 706 new ResolutionRegistry(compiler, |
712 new ResolutionRegistry(compiler, new CollectingTreeElements(element)), | 707 new CollectingTreeElements(element))); |
713 scope: classElement.buildScope()); | |
714 new InitializerResolver(visitor, element, tree).resolveInitializers(); | 708 new InitializerResolver(visitor, element, tree).resolveInitializers(); |
715 visitor.visit(tree.body); | 709 visitor.visit(tree.body); |
716 Expect.equals(expectedElementCount, map(visitor).length, | 710 Expect.equals(expectedElementCount, map(visitor).length, |
717 "${map(visitor).values} for '$statement' in context of `$script`"); | 711 "${map(visitor).values} for '$statement' in context of `$script`"); |
718 | 712 |
719 DiagnosticCollector collector = compiler.diagnosticCollector; | 713 DiagnosticCollector collector = compiler.diagnosticCollector; |
720 compareWarningKinds(script, expectedWarnings, collector.warnings); | 714 compareWarningKinds(script, expectedWarnings, collector.warnings); |
721 compareWarningKinds(script, expectedErrors, collector.errors); | 715 compareWarningKinds(script, expectedErrors, collector.errors); |
722 compareWarningKinds(script, expectedInfos, collector.infos); | 716 compareWarningKinds(script, expectedInfos, collector.infos); |
723 }); | 717 }); |
(...skipping 723 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1447 } | 1441 } |
1448 main() => A.m(); | 1442 main() => A.m(); |
1449 ''', functionName: 'm'); | 1443 ''', functionName: 'm'); |
1450 check(''' | 1444 check(''' |
1451 class A { | 1445 class A { |
1452 m() => () => await - 3; | 1446 m() => () => await - 3; |
1453 } | 1447 } |
1454 main() => new A().m(); | 1448 main() => new A().m(); |
1455 ''', className: 'A'); | 1449 ''', className: 'A'); |
1456 } | 1450 } |
OLD | NEW |