OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 // Separate entrypoint for the frog compiler with experimental support for the | |
6 // 'await' keyword. | |
7 | |
8 #import('../lang.dart'); | |
9 #import('../minfrog.dart', prefix:'minfrog'); | |
10 #source('nodeset.dart'); | |
11 #source('checker.dart'); | |
12 #source('normalizer.dart'); | |
13 #source('transformation.dart'); | |
14 | |
15 /** | |
16 * The main entry point method - needed to ensure we handle correctly await | |
17 * expressions within main. | |
18 */ | |
19 Member _mainMethod; | |
20 | |
21 /** | |
22 * A phase in the compilation process that processes the entire AST and | |
23 * desugars await expressions. | |
24 */ | |
25 awaitTransformation() { | |
26 _mainMethod = | |
27 world.findMainMethod(world.getOrAddLibrary(options.dartScript)); | |
28 for (var lib in world.libraries.getValues()) { | |
29 for (var type in lib.types.getValues()) { | |
30 for (var member in type.members.getValues()) { | |
31 _process(member); | |
32 } | |
33 for (var member in type.constructors.getValues()) { | |
34 _process(member); | |
35 } | |
36 } | |
37 } | |
38 } | |
39 | |
40 /** Analyze and transform a single member (method or property). */ | |
41 _process(Member member) { | |
42 if (member.isConstructor || member.isMethod) { | |
43 _processFunction(member.definition); | |
44 } else if (member.isProperty) { | |
45 PropertyMember p = member; | |
46 if (p.getter != null) _process(p.getter); | |
47 if (p.setter != null) _process(p.setter); | |
48 } | |
49 } | |
50 | |
51 /** Analyze and transform a function definition and nested definitions. */ | |
52 _processFunction(FunctionDefinition func) { | |
53 AwaitChecker checker = new AwaitChecker(); | |
54 | |
55 // Run checker that collects nested functions and which nodes may contain | |
56 // await expressions. | |
57 func.visit(checker); | |
58 | |
59 // Rewrite nested asynchronous functions first. | |
60 for (FunctionDefinition f in checker.nestedFunctions) { | |
61 _processFunction(f); | |
62 } | |
63 | |
64 if (checker.haveAwait.contains(func)) { | |
65 // Normalize | |
66 func.visit(new AwaitNormalizer(checker.haveAwait)); | |
67 // Re-run the checker to derive appropriate node information (this | |
68 // makes the normalizer simpler as we don't have to keep track of this | |
69 // information). | |
70 checker = new AwaitChecker(); | |
71 func.visit(checker); | |
72 | |
73 // Transform the code. | |
74 func.visit(new AwaitProcessor(checker.haveAwait)); | |
75 } | |
76 } | |
77 | |
78 /** Run frog, setting the await transformation correctly. */ | |
79 void main() { | |
80 experimentalAwaitPhase = () { | |
81 world.withTiming('remove await expressions', awaitTransformation); | |
82 }; | |
83 minfrog.main(); | |
84 } | |
OLD | NEW |