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 library dart2js.kernel.element_map; | 5 library dart2js.kernel.element_map; |
6 | 6 |
7 import 'package:kernel/ast.dart' as ir; | 7 import 'package:kernel/ast.dart' as ir; |
8 | 8 |
9 import '../common.dart'; | 9 import '../common.dart'; |
10 import '../common/names.dart' show Identifiers; | 10 import '../common/names.dart' show Identifiers; |
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
656 name = path.substring(path.lastIndexOf('/') + 1); | 656 name = path.substring(path.lastIndexOf('/') + 1); |
657 } | 657 } |
658 LibraryEntity library = | 658 LibraryEntity library = |
659 createLibrary(_libraryMap.length, name, canonicalUri); | 659 createLibrary(_libraryMap.length, name, canonicalUri); |
660 _libraryList.add(library); | 660 _libraryList.add(library); |
661 _libraryData.add(new LibraryData(node)); | 661 _libraryData.add(new LibraryData(node)); |
662 return library; | 662 return library; |
663 }); | 663 }); |
664 } | 664 } |
665 | 665 |
666 // TODO(johnniwinther,efortuna): Create the class index and data together with | |
667 // the [KernelClosureClass]. | |
668 void addClosureClass(KernelClosureClass cls, InterfaceType supertype) { | |
669 cls.classIndex = _classEnvs.length; | |
670 _classEnvs.add(new ClassEnv.closureClass()); | |
671 _classList.add(cls); | |
672 | |
673 // Create a classData and set up the interfaces and subclass | |
674 // relationships that _ensureSupertypes and _ensureThisAndRawType are doing | |
675 var closureData = | |
676 new ClassData(null, new ClosureClassDefinition(cls, cls.location)); | |
677 closureData | |
678 ..isMixinApplication = false | |
679 ..thisType = | |
680 closureData.rawType = new InterfaceType(cls, const/*<DartType>*/ []) | |
681 ..supertype = supertype | |
682 ..interfaces = const <InterfaceType>[]; | |
683 var setBuilder = | |
684 new _KernelOrderedTypeSetBuilder((this as KernelToElementMapBase), cls); | |
685 _classData.add(closureData); | |
686 closureData.orderedTypeSet = setBuilder.createOrderedTypeSet( | |
687 closureData.supertype, const Link<InterfaceType>()); | |
688 | |
689 cls.forEachCapturedVariable((Local local, JField field) { | |
690 field.setClosureMemberIndex = _memberData.length; | |
691 // TODO(efortuna): Uncomment this line after Johnni's added in his CL | |
692 // about Class/MemberDefinition. | |
693 //_memberData.add(field); | |
694 }); | |
695 // TODO(efortuna): Does getMetadata get called in ClassData for this object? | |
696 } | |
697 | |
698 ClassEntity _getClass(ir.Class node, [ClassEnv classEnv]) { | 666 ClassEntity _getClass(ir.Class node, [ClassEnv classEnv]) { |
699 return _classMap.putIfAbsent(node, () { | 667 return _classMap.putIfAbsent(node, () { |
700 KLibrary library = _getLibrary(node.enclosingLibrary); | 668 KLibrary library = _getLibrary(node.enclosingLibrary); |
701 if (classEnv == null) { | 669 if (classEnv == null) { |
702 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name); | 670 classEnv = _libraryEnvs[library.libraryIndex].lookupClass(node.name); |
703 } | 671 } |
704 _classEnvs.add(classEnv); | 672 _classEnvs.add(classEnv); |
705 ClassEntity cls = createClass(library, _classList.length, node.name, | 673 ClassEntity cls = createClass(library, _classList.length, node.name, |
706 isAbstract: node.isAbstract); | 674 isAbstract: node.isAbstract); |
707 _classData | 675 _classData |
(...skipping 1284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1992 // The constructor is not live. | 1960 // The constructor is not live. |
1993 return; | 1961 return; |
1994 } | 1962 } |
1995 ConstructorData data = _memberData[constructor.memberIndex]; | 1963 ConstructorData data = _memberData[constructor.memberIndex]; |
1996 if (data.constructorBody != null) { | 1964 if (data.constructorBody != null) { |
1997 f(data.constructorBody); | 1965 f(data.constructorBody); |
1998 } | 1966 } |
1999 }); | 1967 }); |
2000 } | 1968 } |
2001 | 1969 |
1970 KernelClosureClass constructClosureClass( | |
1971 String name, | |
1972 JLibrary enclosingLibrary, | |
1973 KernelScopeInfo info, | |
1974 ir.Location location, | |
1975 KernelToLocalsMap localsMap, | |
1976 InterfaceType supertype) { | |
1977 KernelClosureClass cls = new KernelClosureClass.fromScopeInfo( | |
1978 name, _classEnvs.length, enclosingLibrary, info, location, localsMap); | |
1979 _classList.add(cls); | |
1980 _classEnvs.add(new ClassEnv.closureClass()); | |
1981 | |
1982 // Create a classData and set up the interfaces and subclass | |
1983 // relationships that _ensureSupertypes and _ensureThisAndRawType are doing | |
1984 var closureData = | |
1985 new ClassData(null, new ClosureClassDefinition(cls, cls.location)); | |
1986 closureData | |
1987 ..isMixinApplication = false | |
1988 ..thisType = | |
1989 closureData.rawType = new InterfaceType(cls, const/*<DartType>*/ []) | |
1990 ..supertype = supertype | |
1991 ..interfaces = const <InterfaceType>[]; | |
1992 var setBuilder = new _KernelOrderedTypeSetBuilder(this, cls); | |
1993 _classData.add(closureData); | |
1994 closureData.orderedTypeSet = setBuilder.createOrderedTypeSet( | |
1995 closureData.supertype, const Link<InterfaceType>()); | |
1996 | |
1997 for (ir.VariableDeclaration variable in info.freeVariables) { | |
1998 // Make a corresponding field entity in this closure class for every | |
1999 // single freeVariable in the KernelScopeInfo.freeVariable. | |
2000 int i = 0; | |
sra1
2017/07/27 19:41:16
should this be outside the loop?
Emily Fortuna
2017/07/28 20:10:04
doh. yes
| |
2001 _constructClosureFields(cls, variable, i, localsMap); | |
2002 i++; | |
2003 } | |
2004 | |
2005 // TODO(efortuna): Does getMetadata get called in ClassData for this object? | |
2006 return cls; | |
2007 } | |
2008 | |
2009 _constructClosureFields( | |
2010 KernelClosureClass cls, | |
2011 ir.VariableDeclaration variable, | |
2012 int fieldNumber, | |
2013 KernelToLocalsMap localsMap) { | |
2014 // NOTE: This construction order may be slightly different than the | |
2015 // old Element version. The old version did all the boxed items and then | |
2016 // all the others. | |
2017 Local capturedLocal = localsMap.getLocalVariable(variable); | |
2018 if (cls.isBoxed(capturedLocal)) { | |
2019 // TODO(efortuna): Coming soon. | |
2020 } else { | |
2021 cls.localToFieldMap[capturedLocal] = new ClosureField( | |
2022 _getClosureVariableName(capturedLocal.name, fieldNumber), | |
2023 _memberData.length, | |
2024 cls, | |
2025 variable.isConst, | |
2026 variable.isFinal || variable.isConst); | |
2027 // PROBLEM: the type of ir.Node that we want to pass in | |
Emily Fortuna
2017/07/27 16:40:02
Johnni, please take a look at this issue. What do
Johnni Winther
2017/07/28 15:18:40
You need to add the field to `_memberList` as well
Emily Fortuna
2017/07/28 20:10:04
ahhh got it. Yeah, that branch shouldn't be hit no
| |
2028 // ClosureMemberDefinition is a *field*, not a VariableDeclaration, so | |
2029 // that in the build() method of builder_kernel.dart we can generate the | |
2030 // correct code. But that requires inventing a fake ir.Field..... | |
2031 // TODO(efortuna): Talk to johnniwinther. | |
2032 //_memberData.add(new MemberData(null, new ClosureMemberDefinition( | |
2033 // cls.localToFieldMap[capturedLocal], variable.location, | |
2034 // MemberKind.closureField, variable))); | |
2035 } | |
2036 } | |
2037 | |
2038 /// Generate a unique name for the [id]th closure field, with proposed name | |
2039 /// [name]. | |
2040 /// | |
2041 /// The result is used as the name of [ClosureFieldElement]s, and must | |
2042 /// therefore be unique to avoid breaking an invariant in the element model | |
2043 /// (classes cannot declare multiple fields with the same name). | |
2044 /// | |
2045 /// Also, the names should be distinct from real field names to prevent | |
2046 /// clashes with selectors for those fields. | |
2047 /// | |
2048 /// These names are not used in generated code, just as element name. | |
2049 String _getClosureVariableName(String name, int id) { | |
2050 return "_captured_${name}_$id"; | |
2051 } | |
2052 | |
2002 String getDeferredUri(ir.LibraryDependency node) { | 2053 String getDeferredUri(ir.LibraryDependency node) { |
2003 throw new UnimplementedError('JsKernelToElementMap.getDeferredUri'); | 2054 throw new UnimplementedError('JsKernelToElementMap.getDeferredUri'); |
2004 } | 2055 } |
2005 } | 2056 } |
OLD | NEW |