| 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 part of dart2js; | 5 part of dart2js; |
| 6 | 6 |
| 7 /// Enum for the visit methods added in [ResolvedVisitor]. | 7 /// Enum for the visit methods added in [ResolvedVisitor]. |
| 8 // TODO(johnniwinther): Remove this. | 8 // TODO(johnniwinther): Remove this. |
| 9 enum ResolvedKind { | 9 enum ResolvedKind { |
| 10 ASSERT, | 10 ASSERT, |
| 11 TYPE_LITERAL, | 11 TYPE_LITERAL, |
| 12 SUPER, | 12 SUPER, |
| 13 OPERATOR, | 13 OPERATOR, |
| 14 TYPE_PREFIX, | 14 TYPE_PREFIX, |
| 15 GETTER, | 15 GETTER, |
| 16 STATIC, | 16 STATIC, |
| 17 CLOSURE, | 17 CLOSURE, |
| 18 DYNAMIC, | 18 DYNAMIC, |
| 19 ERROR, | 19 ERROR, |
| 20 SEND_SET, | |
| 21 NEW, | |
| 22 } | 20 } |
| 23 | 21 |
| 24 /// Abstract interface for a [ResolvedVisitor]. | 22 /// Abstract interface for a [ResolvedVisitor]. |
| 25 // TODO(johnniwinther): Remove this. | 23 // TODO(johnniwinther): Remove this. |
| 26 abstract class ResolvedKindVisitor<R> { | 24 abstract class ResolvedKindVisitor<R> { |
| 27 R visitSuperSend(Send node); | 25 R visitSuperSend(Send node); |
| 28 R visitOperatorSend(Send node); | 26 R visitOperatorSend(Send node); |
| 29 R visitGetterSend(Send node); | 27 R visitGetterSend(Send node); |
| 30 R visitClosureSend(Send node); | 28 R visitClosureSend(Send node); |
| 31 R visitDynamicSend(Send node); | 29 R visitDynamicSend(Send node); |
| 32 R visitStaticSend(Send node); | 30 R visitStaticSend(Send node); |
| 33 R handleSendSet(SendSet node); | |
| 34 R handleNewExpression(NewExpression node); | |
| 35 | 31 |
| 36 /// Visitor callback for a type literal. | 32 /// Visitor callback for a type literal. |
| 37 R visitTypeLiteralSend(Send node); | 33 R visitTypeLiteralSend(Send node); |
| 38 | 34 |
| 39 /// Visitor callback for the class prefix of a static access, like `Foo` in | 35 /// Visitor callback for the class prefix of a static access, like `Foo` in |
| 40 /// `Foo.staticField`. | 36 /// `Foo.staticField`. |
| 41 // TODO(johnniwinther): Remove this when not needed by the dart backend. | 37 // TODO(johnniwinther): Remove this when not needed by the dart backend. |
| 42 R visitTypePrefixSend(Send node); | 38 R visitTypePrefixSend(Send node); |
| 43 | 39 |
| 44 R visitAssertSend(Send node); | 40 R visitAssertSend(Send node); |
| 45 | 41 |
| 46 internalError(Spannable node, String reason); | 42 internalError(Spannable node, String reason); |
| 47 } | 43 } |
| 48 | 44 |
| 49 /// Visitor that returns the [ResolvedKind] corresponding to the called visitor | 45 /// Visitor that returns the [ResolvedKind] corresponding to the called visitor |
| 50 /// method. | 46 /// method. |
| 51 class ResolvedKindComputer implements ResolvedKindVisitor { | 47 class ResolvedKindComputer implements ResolvedKindVisitor { |
| 52 const ResolvedKindComputer(); | 48 const ResolvedKindComputer(); |
| 53 | 49 |
| 54 ResolvedKind visitSuperSend(Send node) => ResolvedKind.SUPER; | 50 ResolvedKind visitSuperSend(Send node) => ResolvedKind.SUPER; |
| 55 ResolvedKind visitOperatorSend(Send node) => ResolvedKind.OPERATOR; | 51 ResolvedKind visitOperatorSend(Send node) => ResolvedKind.OPERATOR; |
| 56 ResolvedKind visitGetterSend(Send node) => ResolvedKind.GETTER; | 52 ResolvedKind visitGetterSend(Send node) => ResolvedKind.GETTER; |
| 57 ResolvedKind visitClosureSend(Send node) => ResolvedKind.CLOSURE; | 53 ResolvedKind visitClosureSend(Send node) => ResolvedKind.CLOSURE; |
| 58 ResolvedKind visitDynamicSend(Send node) => ResolvedKind.DYNAMIC; | 54 ResolvedKind visitDynamicSend(Send node) => ResolvedKind.DYNAMIC; |
| 59 ResolvedKind visitStaticSend(Send node) => ResolvedKind.STATIC; | 55 ResolvedKind visitStaticSend(Send node) => ResolvedKind.STATIC; |
| 60 ResolvedKind visitTypeLiteralSend(Send node) => ResolvedKind.TYPE_LITERAL; | 56 ResolvedKind visitTypeLiteralSend(Send node) => ResolvedKind.TYPE_LITERAL; |
| 61 ResolvedKind visitTypePrefixSend(Send node) => ResolvedKind.TYPE_PREFIX; | 57 ResolvedKind visitTypePrefixSend(Send node) => ResolvedKind.TYPE_PREFIX; |
| 62 ResolvedKind visitAssertSend(Send node) => ResolvedKind.ASSERT; | 58 ResolvedKind visitAssertSend(Send node) => ResolvedKind.ASSERT; |
| 63 ResolvedKind handleSendSet(SendSet node) => ResolvedKind.SEND_SET; | |
| 64 ResolvedKind handleNewExpression(NewExpression node) => ResolvedKind.NEW; | |
| 65 internalError(Spannable node, String reason) => ResolvedKind.ERROR; | 59 internalError(Spannable node, String reason) => ResolvedKind.ERROR; |
| 66 } | 60 } |
| 67 | 61 |
| 68 abstract class ResolvedVisitor<R> | 62 abstract class ResolvedVisitor<R> |
| 69 implements Visitor<R>, ResolvedKindVisitor<R> {} | 63 implements Visitor<R>, ResolvedKindVisitor<R> {} |
| 70 | 64 |
| 71 abstract class BaseResolvedVisitor<R> extends Visitor<R> | 65 abstract class BaseResolvedVisitor<R> extends Visitor<R> |
| 72 implements ResolvedVisitor<R> { | 66 implements ResolvedVisitor<R> { |
| 73 | 67 |
| 74 TreeElements elements; | 68 TreeElements elements; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 } | 123 } |
| 130 | 124 |
| 131 // TODO(johnniwinther): Remove this. Currently need by the old dart2dart | 125 // TODO(johnniwinther): Remove this. Currently need by the old dart2dart |
| 132 // backend. | 126 // backend. |
| 133 abstract class OldResolvedVisitor<R> extends BaseResolvedVisitor<R> { | 127 abstract class OldResolvedVisitor<R> extends BaseResolvedVisitor<R> { |
| 134 OldResolvedVisitor(TreeElements elements) : super(elements); | 128 OldResolvedVisitor(TreeElements elements) : super(elements); |
| 135 | 129 |
| 136 R visitSend(Send node) { | 130 R visitSend(Send node) { |
| 137 return _oldDispatch(node, this); | 131 return _oldDispatch(node, this); |
| 138 } | 132 } |
| 139 | |
| 140 R visitSendSet(SendSet node) { | |
| 141 return handleSendSet(node); | |
| 142 } | |
| 143 | |
| 144 R visitNewExpression(NewExpression node) { | |
| 145 return handleNewExpression(node); | |
| 146 } | |
| 147 } | 133 } |
| 148 | 134 |
| 149 abstract class NewResolvedVisitor<R> extends BaseResolvedVisitor<R> | 135 abstract class NewResolvedVisitor<R> extends BaseResolvedVisitor<R> |
| 150 with SendResolverMixin, | 136 with SendResolverMixin, |
| 151 GetBulkMixin<R, dynamic>, | 137 GetBulkMixin<R, dynamic>, |
| 152 SetBulkMixin<R, dynamic>, | 138 SetBulkMixin<R, dynamic>, |
| 153 ErrorBulkMixin<R, dynamic>, | 139 ErrorBulkMixin<R, dynamic>, |
| 154 InvokeBulkMixin<R, dynamic>, | 140 InvokeBulkMixin<R, dynamic>, |
| 155 IndexSetBulkMixin<R, dynamic>, | 141 IndexSetBulkMixin<R, dynamic>, |
| 156 CompoundBulkMixin<R, dynamic>, | 142 CompoundBulkMixin<R, dynamic>, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 if (sendStructure != null) { | 178 if (sendStructure != null) { |
| 193 var arg = sendVisitor == _resolvedKindDispatcher | 179 var arg = sendVisitor == _resolvedKindDispatcher |
| 194 ? kindVisitor : sendStructure; | 180 ? kindVisitor : sendStructure; |
| 195 return sendStructure.dispatch(sendVisitor, node, arg); | 181 return sendStructure.dispatch(sendVisitor, node, arg); |
| 196 } else { | 182 } else { |
| 197 return kindVisitor.visitStaticSend(node); | 183 return kindVisitor.visitStaticSend(node); |
| 198 } | 184 } |
| 199 } | 185 } |
| 200 } | 186 } |
| 201 | 187 |
| 202 bool checkResolvedKind(Node node, | |
| 203 ResolvedKind oldKind, | |
| 204 ResolvedKind newKind) { | |
| 205 return invariant(node, oldKind == newKind, message: '$oldKind != $newKind'); | |
| 206 } | |
| 207 | |
| 208 ResolvedKind computeResolvedKindFromStructure( | |
| 209 Node node, SemanticSendStructure structure) { | |
| 210 return structure.dispatch( | |
| 211 _resolvedKindDispatcher, node, const ResolvedKindComputer()); | |
| 212 } | |
| 213 | |
| 214 @override | |
| 215 R visitSend(Send node) { | 188 R visitSend(Send node) { |
| 216 assert(checkResolvedKind( | 189 ResolvedKind oldKind; |
| 217 node, | 190 ResolvedKind newKind; |
| 218 _oldDispatch(node, const ResolvedKindComputer()), | 191 assert(invariant(node, () { |
| 219 _newDispatch(node, const ResolvedKindComputer(), | 192 oldKind = _oldDispatch(node, const ResolvedKindComputer()); |
| 220 _resolvedKindDispatcher))); | 193 newKind = _newDispatch( |
| 194 node, const ResolvedKindComputer(), _resolvedKindDispatcher); |
| 195 return oldKind == newKind; |
| 196 }, message: () => '$oldKind != $newKind')); |
| 221 return _newDispatch(node, this, this); | 197 return _newDispatch(node, this, this); |
| 222 } | 198 } |
| 223 | 199 |
| 224 @override | 200 @override |
| 225 R visitSendSet(Send node) { | |
| 226 SendStructure structure = computeSendStructure(node); | |
| 227 if (structure == null) { | |
| 228 return internalError(node, 'No structure for $node'); | |
| 229 } else { | |
| 230 assert(checkResolvedKind(node, | |
| 231 ResolvedKind.SEND_SET, | |
| 232 computeResolvedKindFromStructure(node, structure))); | |
| 233 return structure.dispatch(this, node, structure); | |
| 234 } | |
| 235 } | |
| 236 | |
| 237 @override | |
| 238 R visitNewExpression(NewExpression node) { | |
| 239 NewStructure structure = computeNewStructure(node); | |
| 240 if (structure == null) { | |
| 241 return internalError(node, 'No structure for $node'); | |
| 242 } else { | |
| 243 assert(checkResolvedKind(node, | |
| 244 ResolvedKind.NEW, | |
| 245 computeResolvedKindFromStructure(node, structure))); | |
| 246 return structure.dispatch(this, node, structure); | |
| 247 } | |
| 248 } | |
| 249 | |
| 250 @override | |
| 251 R apply(Node node, arg) { | 201 R apply(Node node, arg) { |
| 252 return visitNode(node); | 202 return visitNode(node); |
| 253 } | 203 } |
| 254 | 204 |
| 255 @override | 205 @override |
| 256 R bulkHandleNode( | 206 R bulkHandleNode( |
| 257 Node node, | 207 Node node, |
| 258 String message, | 208 String message, |
| 259 SemanticSendStructure structure) { | 209 SendStructure sendStructure) { |
| 260 return structure.dispatch(_semanticDispatcher, node, this); | 210 return sendStructure.dispatch(_semanticDispatcher, node, this); |
| 261 } | 211 } |
| 262 } | 212 } |
| 263 | 213 |
| 264 /// Visitor that dispatches [SemanticSendVisitor] calls to the corresponding | 214 /// Visitor that dispatches [SemanticSendVisitor] calls to the corresponding |
| 265 /// visit methods in [ResolvedVisitor]. | 215 /// visit methods in [ResolvedVisitor]. |
| 266 class ResolvedSemanticDispatcher<R> extends Object | 216 class ResolvedSemanticDispatcher<R> extends Object |
| 267 with GetBulkMixin<R, ResolvedKindVisitor<R>>, | 217 with GetBulkMixin<R, ResolvedKindVisitor<R>>, |
| 268 SetBulkMixin<R, ResolvedKindVisitor<R>>, | 218 SetBulkMixin<R, ResolvedKindVisitor<R>>, |
| 269 InvokeBulkMixin<R, ResolvedKindVisitor<R>>, | 219 InvokeBulkMixin<R, ResolvedKindVisitor<R>>, |
| 270 PrefixBulkMixin<R, ResolvedKindVisitor<R>>, | 220 PrefixBulkMixin<R, ResolvedKindVisitor<R>>, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 282 R apply(Node node, ResolvedKindVisitor<R> visitor) { | 232 R apply(Node node, ResolvedKindVisitor<R> visitor) { |
| 283 return visitor.internalError( | 233 return visitor.internalError( |
| 284 node, "ResolvedSemanticDispatcher.apply unsupported."); | 234 node, "ResolvedSemanticDispatcher.apply unsupported."); |
| 285 } | 235 } |
| 286 | 236 |
| 287 @override | 237 @override |
| 288 R bulkHandleNode( | 238 R bulkHandleNode( |
| 289 Node node, | 239 Node node, |
| 290 String message, | 240 String message, |
| 291 ResolvedKindVisitor<R> visitor) { | 241 ResolvedKindVisitor<R> visitor) { |
| 242 // Set, Compound, IndexSet, and NewExpression are not handled by |
| 243 // [ResolvedVisitor]. |
| 292 return bulkHandleError(node, visitor); | 244 return bulkHandleError(node, visitor); |
| 293 } | 245 } |
| 294 | 246 |
| 295 R bulkHandleError(Node node, ResolvedKindVisitor<R> visitor) { | 247 R bulkHandleError(Node node, ResolvedKindVisitor<R> visitor) { |
| 296 if (node.asSendSet() != null) { | 248 return visitor.internalError(node, "No resolved kind for node."); |
| 297 return visitor.handleSendSet(node); | |
| 298 } else if (node.asNewExpression() != null) { | |
| 299 return visitor.handleNewExpression(node); | |
| 300 } | |
| 301 return visitor.internalError(node, "No resolved kind for $node."); | |
| 302 } | 249 } |
| 303 | 250 |
| 304 @override | 251 @override |
| 305 R bulkHandleGet(Node node, ResolvedKindVisitor<R> visitor) { | 252 R bulkHandleGet(Node node, ResolvedKindVisitor<R> visitor) { |
| 306 return visitor.visitGetterSend(node); | 253 return visitor.visitGetterSend(node); |
| 307 } | 254 } |
| 308 | 255 |
| 309 @override | 256 @override |
| 310 R bulkHandleInvoke(Node node, ResolvedKindVisitor<R> visitor) { | 257 R bulkHandleInvoke(Node node, ResolvedKindVisitor<R> visitor) { |
| 311 // Most invokes are static. | 258 // Most invokes are static. |
| 312 return visitor.visitStaticSend(node); | 259 return visitor.visitStaticSend(node); |
| 313 } | 260 } |
| 314 | 261 |
| 315 @override | 262 @override |
| 316 R bulkHandlePrefix(Node node, ResolvedKindVisitor<R> visitor) { | 263 R bulkHandlePrefix(Node node, ResolvedKindVisitor<R> visitor) { |
| 317 return visitor.handleSendSet(node); | 264 return visitor.visitOperatorSend(node); |
| 318 } | 265 } |
| 319 | 266 |
| 320 @override | 267 @override |
| 321 R bulkHandlePostfix(Node node, ResolvedKindVisitor<R> visitor) { | 268 R bulkHandlePostfix(Node node, ResolvedKindVisitor<R> visitor) { |
| 322 return visitor.handleSendSet(node); | 269 return visitor.visitOperatorSend(node); |
| 323 } | 270 } |
| 324 | 271 |
| 325 @override | 272 @override |
| 326 R bulkHandleSuper(Node node, ResolvedKindVisitor<R> visitor) { | 273 R bulkHandleSuper(Node node, ResolvedKindVisitor<R> visitor) { |
| 327 if (node.asSendSet() != null) { | |
| 328 return visitor.handleSendSet(node); | |
| 329 } | |
| 330 return visitor.visitSuperSend(node); | 274 return visitor.visitSuperSend(node); |
| 331 } | 275 } |
| 332 | 276 |
| 333 @override | 277 @override |
| 334 R bulkHandleSet(SendSet node, ResolvedKindVisitor<R> visitor) { | |
| 335 return visitor.handleSendSet(node); | |
| 336 } | |
| 337 | |
| 338 @override | |
| 339 R bulkHandleCompound(SendSet node, ResolvedKindVisitor<R> visitor) { | |
| 340 return visitor.handleSendSet(node); | |
| 341 } | |
| 342 | |
| 343 @override | |
| 344 R bulkHandleIndexSet(SendSet node, ResolvedKindVisitor<R> visitor) { | |
| 345 return visitor.handleSendSet(node); | |
| 346 } | |
| 347 | |
| 348 @override | |
| 349 R bulkHandleNew(NewExpression node, ResolvedKindVisitor<R> visitor) { | |
| 350 return visitor.handleNewExpression(node); | |
| 351 } | |
| 352 | |
| 353 @override | |
| 354 R errorInvalidAssert( | 278 R errorInvalidAssert( |
| 355 Send node, | 279 Send node, |
| 356 NodeList arguments, | 280 NodeList arguments, |
| 357 ResolvedKindVisitor<R> visitor) { | 281 ResolvedKindVisitor<R> visitor) { |
| 358 return visitor.visitAssertSend(node); | 282 return visitor.visitAssertSend(node); |
| 359 } | 283 } |
| 360 | 284 |
| 361 @override | 285 @override |
| 362 R errorLocalFunctionPostfix( | 286 R errorLocalFunctionPostfix( |
| 363 Send node, | 287 Send node, |
| (...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 774 | 698 |
| 775 @override | 699 @override |
| 776 R errorUnresolvedSuperIndex( | 700 R errorUnresolvedSuperIndex( |
| 777 Send node, | 701 Send node, |
| 778 Element function, | 702 Element function, |
| 779 Node index, | 703 Node index, |
| 780 ResolvedKindVisitor<R> visitor) { | 704 ResolvedKindVisitor<R> visitor) { |
| 781 return visitor.visitSuperSend(node); | 705 return visitor.visitSuperSend(node); |
| 782 } | 706 } |
| 783 } | 707 } |
| OLD | NEW |