OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.resolution.common; | 5 library dart2js.resolution.common; |
6 | 6 |
7 import '../common.dart'; | 7 import '../common.dart'; |
8 import '../common/resolution.dart' show | 8 import '../common/resolution.dart' show Resolution; |
9 Resolution; | 9 import '../common/tasks.dart' show DeferredAction; |
10 import '../common/tasks.dart' show | 10 import '../compiler.dart' show Compiler; |
11 DeferredAction; | |
12 import '../compiler.dart' show | |
13 Compiler; | |
14 import '../elements/elements.dart'; | 11 import '../elements/elements.dart'; |
15 import '../tree/tree.dart'; | 12 import '../tree/tree.dart'; |
16 | 13 |
17 import 'registry.dart' show | 14 import 'registry.dart' show ResolutionRegistry; |
18 ResolutionRegistry; | 15 import 'scope.dart' show Scope; |
19 import 'scope.dart' show | 16 import 'type_resolver.dart' show TypeResolver; |
20 Scope; | |
21 import 'type_resolver.dart' show | |
22 TypeResolver; | |
23 | 17 |
24 class CommonResolverVisitor<R> extends Visitor<R> { | 18 class CommonResolverVisitor<R> extends Visitor<R> { |
25 final Compiler compiler; | 19 final Compiler compiler; |
26 | 20 |
27 CommonResolverVisitor(Compiler this.compiler); | 21 CommonResolverVisitor(Compiler this.compiler); |
28 | 22 |
29 DiagnosticReporter get reporter => compiler.reporter; | 23 DiagnosticReporter get reporter => compiler.reporter; |
30 | 24 |
31 Resolution get resolution => compiler.resolution; | 25 Resolution get resolution => compiler.resolution; |
32 | 26 |
33 R visitNode(Node node) { | 27 R visitNode(Node node) { |
34 return reporter.internalError(node, | 28 return reporter.internalError( |
35 'internal error: Unhandled node: ${node.getObjectDescription()}'); | 29 node, 'internal error: Unhandled node: ${node.getObjectDescription()}'); |
36 } | 30 } |
37 | 31 |
38 R visitEmptyStatement(Node node) => null; | 32 R visitEmptyStatement(Node node) => null; |
39 | 33 |
40 /** Convenience method for visiting nodes that may be null. */ | 34 /** Convenience method for visiting nodes that may be null. */ |
41 R visit(Node node) => (node == null) ? null : node.accept(this); | 35 R visit(Node node) => (node == null) ? null : node.accept(this); |
42 | 36 |
43 void addDeferredAction(Element element, DeferredAction action) { | 37 void addDeferredAction(Element element, DeferredAction action) { |
44 compiler.enqueuer.resolution.addDeferredAction(element, action); | 38 compiler.enqueuer.resolution.addDeferredAction(element, action); |
45 } | 39 } |
46 } | 40 } |
47 | 41 |
48 /** | 42 /** |
49 * Common supertype for resolver visitors that record resolutions in a | 43 * Common supertype for resolver visitors that record resolutions in a |
50 * [ResolutionRegistry]. | 44 * [ResolutionRegistry]. |
51 */ | 45 */ |
52 abstract class MappingVisitor<T> extends CommonResolverVisitor<T> { | 46 abstract class MappingVisitor<T> extends CommonResolverVisitor<T> { |
53 final ResolutionRegistry registry; | 47 final ResolutionRegistry registry; |
54 final TypeResolver typeResolver; | 48 final TypeResolver typeResolver; |
| 49 |
55 /// The current enclosing element for the visited AST nodes. | 50 /// The current enclosing element for the visited AST nodes. |
56 Element get enclosingElement; | 51 Element get enclosingElement; |
| 52 |
57 /// The current scope of the visitor. | 53 /// The current scope of the visitor. |
58 Scope get scope; | 54 Scope get scope; |
59 | 55 |
60 MappingVisitor(Compiler compiler, ResolutionRegistry this.registry) | 56 MappingVisitor(Compiler compiler, ResolutionRegistry this.registry) |
61 : typeResolver = new TypeResolver(compiler), | 57 : typeResolver = new TypeResolver(compiler), |
62 super(compiler); | 58 super(compiler); |
63 | 59 |
64 AsyncMarker get currentAsyncMarker => AsyncMarker.SYNC; | 60 AsyncMarker get currentAsyncMarker => AsyncMarker.SYNC; |
65 | 61 |
66 /// Add [element] to the current scope and check for duplicate definitions. | 62 /// Add [element] to the current scope and check for duplicate definitions. |
67 void addToScope(Element element) { | 63 void addToScope(Element element) { |
68 Element existing = scope.add(element); | 64 Element existing = scope.add(element); |
69 if (existing != element) { | 65 if (existing != element) { |
70 reportDuplicateDefinition(element.name, element, existing); | 66 reportDuplicateDefinition(element.name, element, existing); |
71 } | 67 } |
72 } | 68 } |
73 | 69 |
74 void checkLocalDefinitionName(Node node, Element element) { | 70 void checkLocalDefinitionName(Node node, Element element) { |
75 if (currentAsyncMarker != AsyncMarker.SYNC) { | 71 if (currentAsyncMarker != AsyncMarker.SYNC) { |
76 if (element.name == 'yield' || | 72 if (element.name == 'yield' || |
77 element.name == 'async' || | 73 element.name == 'async' || |
78 element.name == 'await') { | 74 element.name == 'await') { |
79 reporter.reportErrorMessage( | 75 reporter.reportErrorMessage( |
80 node, MessageKind.ASYNC_KEYWORD_AS_IDENTIFIER, | 76 node, |
81 {'keyword': element.name, | 77 MessageKind.ASYNC_KEYWORD_AS_IDENTIFIER, |
82 'modifier': currentAsyncMarker}); | 78 {'keyword': element.name, 'modifier': currentAsyncMarker}); |
83 } | 79 } |
84 } | 80 } |
85 } | 81 } |
86 | 82 |
87 /// Register [node] as the definition of [element]. | 83 /// Register [node] as the definition of [element]. |
88 void defineLocalVariable(Node node, LocalVariableElement element) { | 84 void defineLocalVariable(Node node, LocalVariableElement element) { |
89 if (element == null) { | 85 if (element == null) { |
90 throw reporter.internalError(node, 'element is null'); | 86 throw reporter.internalError(node, 'element is null'); |
91 } | 87 } |
92 checkLocalDefinitionName(node, element); | 88 checkLocalDefinitionName(node, element); |
93 registry.defineElement(node, element); | 89 registry.defineElement(node, element); |
94 } | 90 } |
95 | 91 |
96 void reportDuplicateDefinition(String name, | 92 void reportDuplicateDefinition( |
97 Spannable definition, | 93 String name, Spannable definition, Spannable existing) { |
98 Spannable existing) { | |
99 reporter.reportError( | 94 reporter.reportError( |
100 reporter.createMessage( | 95 reporter.createMessage( |
101 definition, | 96 definition, MessageKind.DUPLICATE_DEFINITION, {'name': name}), |
102 MessageKind.DUPLICATE_DEFINITION, | |
103 {'name': name}), | |
104 <DiagnosticMessage>[ | 97 <DiagnosticMessage>[ |
105 reporter.createMessage( | 98 reporter.createMessage( |
106 existing, | 99 existing, MessageKind.EXISTING_DEFINITION, {'name': name}), |
107 MessageKind.EXISTING_DEFINITION, | |
108 {'name': name}), | |
109 ]); | 100 ]); |
110 } | 101 } |
111 } | 102 } |
OLD | NEW |