OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 'package:kernel/ast.dart' as ir; | 5 import 'package:kernel/ast.dart' as ir; |
6 | 6 |
7 import '../closure.dart'; | 7 import '../closure.dart'; |
8 import '../common.dart'; | 8 import '../common.dart'; |
9 import '../common/tasks.dart'; | 9 import '../common/tasks.dart'; |
| 10 import '../constants/expressions.dart'; |
| 11 import '../constants/values.dart'; |
10 import '../elements/entities.dart'; | 12 import '../elements/entities.dart'; |
11 import '../elements/names.dart' show Name; | 13 import '../elements/names.dart' show Name; |
| 14 import '../elements/types.dart'; |
12 import '../kernel/element_map.dart'; | 15 import '../kernel/element_map.dart'; |
| 16 import '../kernel/env.dart'; |
13 import '../world.dart'; | 17 import '../world.dart'; |
14 import 'elements.dart'; | 18 import 'elements.dart'; |
15 import 'closure_visitors.dart'; | 19 import 'closure_visitors.dart'; |
16 import 'locals.dart'; | 20 import 'locals.dart'; |
17 import 'js_strategy.dart' show JsClosedWorld; | 21 import 'js_strategy.dart' show JsClosedWorld; |
18 | 22 |
19 class KernelClosureAnalysis { | 23 class KernelClosureAnalysis { |
20 /// Inspect members and mark if those members capture any state that needs to | 24 /// Inspect members and mark if those members capture any state that needs to |
21 /// be marked as free variables. | 25 /// be marked as free variables. |
22 static ClosureModel computeClosureModel(MemberEntity entity, ir.Member node) { | 26 static ClosureModel computeClosureModel(MemberEntity entity, ir.Member node) { |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 .map(localsMap.getLocalVariable) | 312 .map(localsMap.getLocalVariable) |
309 .toList(), | 313 .toList(), |
310 super.from(capturedScope, localsMap); | 314 super.from(capturedScope, localsMap); |
311 | 315 |
312 bool get hasBoxedLoopVariables => boxedLoopVariables.isNotEmpty; | 316 bool get hasBoxedLoopVariables => boxedLoopVariables.isNotEmpty; |
313 } | 317 } |
314 | 318 |
315 // TODO(johnniwinther): Add unittest for the computed [ClosureClass]. | 319 // TODO(johnniwinther): Add unittest for the computed [ClosureClass]. |
316 class KernelClosureClass extends JsScopeInfo | 320 class KernelClosureClass extends JsScopeInfo |
317 implements ClosureRepresentationInfo, JClass { | 321 implements ClosureRepresentationInfo, JClass { |
318 final ir.Location location; | |
319 | |
320 final String name; | 322 final String name; |
321 final JLibrary library; | 323 final JLibrary library; |
322 JFunction callMethod; | 324 JFunction callMethod; |
323 final Local closureEntity; | 325 final Local closureEntity; |
324 | 326 |
325 /// Index into the classData, classList and classEnvironment lists where this | 327 /// Index into the classData, classList and classEnvironment lists where this |
326 /// entity is stored in [JsToFrontendMapImpl]. | 328 /// entity is stored in [JsToFrontendMapImpl]. |
327 final int classIndex; | 329 final int classIndex; |
328 | 330 |
329 final Map<Local, JField> localToFieldMap = new Map<Local, JField>(); | 331 final Map<Local, JField> localToFieldMap = new Map<Local, JField>(); |
330 | 332 |
331 KernelClosureClass.fromScopeInfo( | 333 KernelClosureClass.fromScopeInfo( |
332 ir.FunctionNode closureSourceNode, | 334 ir.FunctionNode closureSourceNode, |
333 this.name, | 335 this.name, |
334 this.classIndex, | 336 this.classIndex, |
335 this.library, | 337 this.library, |
336 KernelScopeInfo info, | 338 KernelScopeInfo info, |
337 this.location, | |
338 KernelToLocalsMap localsMap) | 339 KernelToLocalsMap localsMap) |
339 : closureEntity = localsMap.getLocalFunction(closureSourceNode.parent), | 340 : closureEntity = localsMap.getLocalFunction(closureSourceNode.parent), |
340 super.from(info, localsMap); | 341 super.from(info, localsMap); |
341 | 342 |
342 ClassEntity get closureClassEntity => this; | 343 ClassEntity get closureClassEntity => this; |
343 | 344 |
344 List<Local> get createdFieldEntities => localToFieldMap.keys.toList(); | 345 List<Local> get createdFieldEntities => localToFieldMap.keys.toList(); |
345 | 346 |
346 // TODO(efortuna): Implement. | 347 // TODO(efortuna): Implement. |
347 FieldEntity get thisFieldEntity => null; | 348 FieldEntity get thisFieldEntity => null; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 final BoxLocal box; | 401 final BoxLocal box; |
401 JBoxedField(String name, int memberIndex, this.box, | 402 JBoxedField(String name, int memberIndex, this.box, |
402 KernelClosureClass containingClass, bool isConst, bool isAssignable) | 403 KernelClosureClass containingClass, bool isConst, bool isAssignable) |
403 : super(memberIndex, containingClass.library, containingClass, | 404 : super(memberIndex, containingClass.library, containingClass, |
404 new Name(name, containingClass.library), | 405 new Name(name, containingClass.library), |
405 isAssignable: isAssignable, isConst: isConst); | 406 isAssignable: isAssignable, isConst: isConst); |
406 } | 407 } |
407 | 408 |
408 class ClosureClassDefinition implements ClassDefinition { | 409 class ClosureClassDefinition implements ClassDefinition { |
409 final ClassEntity cls; | 410 final ClassEntity cls; |
410 final ir.Location location; | 411 final SourceSpan location; |
411 | 412 |
412 ClosureClassDefinition(this.cls, this.location); | 413 ClosureClassDefinition(this.cls, this.location); |
413 | 414 |
414 ClassKind get kind => ClassKind.closure; | 415 ClassKind get kind => ClassKind.closure; |
415 | 416 |
416 ir.Node get node => | 417 ir.Node get node => |
417 throw new UnsupportedError('ClosureClassDefinition.node for $cls'); | 418 throw new UnsupportedError('ClosureClassDefinition.node for $cls'); |
418 | 419 |
419 String toString() => | 420 String toString() => |
420 'ClosureClassDefinition(kind:$kind,cls:$cls,location:$location)'; | 421 'ClosureClassDefinition(kind:$kind,cls:$cls,location:$location)'; |
421 } | 422 } |
422 | 423 |
| 424 class ClosureMemberData implements MemberData { |
| 425 final MemberDefinition definition; |
| 426 |
| 427 ClosureMemberData(this.definition); |
| 428 |
| 429 @override |
| 430 Iterable<ConstantValue> getMetadata(KernelToElementMap elementMap) { |
| 431 return const <ConstantValue>[]; |
| 432 } |
| 433 } |
| 434 |
| 435 class ClosureFunctionData extends ClosureMemberData implements FunctionData { |
| 436 final FunctionType functionType; |
| 437 |
| 438 ClosureFunctionData(MemberDefinition definition, this.functionType) |
| 439 : super(definition); |
| 440 |
| 441 @override |
| 442 void forEachParameter(KernelToElementMapForBuilding elementMap, |
| 443 void f(DartType type, String name, ConstantValue defaultValue)) { |
| 444 // TODO(johnniwinther,efortuna): Implement this. |
| 445 throw new UnimplementedError('ClosureFunctionData.forEachParameter'); |
| 446 } |
| 447 |
| 448 @override |
| 449 FunctionType getFunctionType(KernelToElementMap elementMap) { |
| 450 return functionType; |
| 451 } |
| 452 } |
| 453 |
| 454 class ClosureFieldData extends ClosureMemberData implements FieldData { |
| 455 ClosureFieldData(MemberDefinition definition) : super(definition); |
| 456 |
| 457 @override |
| 458 ConstantExpression getFieldConstant( |
| 459 KernelToElementMap elementMap, FieldEntity field) { |
| 460 failedAt( |
| 461 field, |
| 462 "Unexpected field $field in " |
| 463 "ClosureFieldData.getFieldConstant"); |
| 464 return null; |
| 465 } |
| 466 } |
| 467 |
423 class ClosureMemberDefinition implements MemberDefinition { | 468 class ClosureMemberDefinition implements MemberDefinition { |
424 final MemberEntity member; | 469 final MemberEntity member; |
425 final ir.Location location; | 470 final SourceSpan location; |
426 final MemberKind kind; | 471 final MemberKind kind; |
427 final ir.Node node; | 472 final ir.Node node; |
428 | 473 |
429 ClosureMemberDefinition(this.member, this.location, this.kind, this.node); | 474 ClosureMemberDefinition(this.member, this.location, this.kind, this.node); |
430 | 475 |
431 String toString() => | 476 String toString() => |
432 'ClosureMemberDefinition(kind:$kind,member:$member,location:$location)'; | 477 'ClosureMemberDefinition(kind:$kind,member:$member,location:$location)'; |
433 } | 478 } |
434 | 479 |
435 /// Collection of closure data collected for a single member. | 480 /// Collection of closure data collected for a single member. |
436 class ClosureModel { | 481 class ClosureModel { |
437 /// Collection [ScopeInfo] data for the member, if any. | 482 /// Collection [ScopeInfo] data for the member, if any. |
438 // TODO(johnniwinther): [scopeInfo] seem to be missing only for fields | 483 // TODO(johnniwinther): [scopeInfo] seem to be missing only for fields |
439 // without initializers; we shouldn't even create a [ClosureModel] in these | 484 // without initializers; we shouldn't even create a [ClosureModel] in these |
440 // cases. | 485 // cases. |
441 KernelScopeInfo scopeInfo; | 486 KernelScopeInfo scopeInfo; |
442 | 487 |
443 /// Collected [CapturedScope] data for nodes. | 488 /// Collected [CapturedScope] data for nodes. |
444 Map<ir.Node, KernelCapturedScope> capturedScopesMap = | 489 Map<ir.Node, KernelCapturedScope> capturedScopesMap = |
445 <ir.Node, KernelCapturedScope>{}; | 490 <ir.Node, KernelCapturedScope>{}; |
446 | 491 |
447 /// Collected [ScopeInfo] data for nodes. | 492 /// Collected [ScopeInfo] data for nodes. |
448 Map<ir.FunctionNode, KernelScopeInfo> closuresToGenerate = | 493 Map<ir.FunctionNode, KernelScopeInfo> closuresToGenerate = |
449 <ir.FunctionNode, KernelScopeInfo>{}; | 494 <ir.FunctionNode, KernelScopeInfo>{}; |
450 } | 495 } |
OLD | NEW |