OLD | NEW |
1 // Copyright (c) 2015, the Fletch project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Fletch 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 library fletchc.compiled_class; | 5 library fletchc.fletch_class_builder; |
6 | 6 |
7 import 'package:compiler/src/dart_types.dart'; | 7 import 'package:compiler/src/dart_types.dart'; |
8 import 'package:compiler/src/elements/elements.dart'; | 8 import 'package:compiler/src/elements/elements.dart'; |
9 import 'package:compiler/src/universe/universe.dart'; | 9 import 'package:compiler/src/universe/universe.dart'; |
10 | 10 |
11 import 'compiled_function.dart' show | 11 import 'fletch_function_builder.dart' show |
12 CompiledFunction; | 12 FletchFunctionBuilder; |
13 | 13 |
14 import 'fletch_backend.dart'; | 14 import 'fletch_backend.dart'; |
15 | 15 |
16 class CompiledClass { | 16 class FletchClassBuilder { |
17 final int id; | 17 final int id; |
18 final ClassElement element; | 18 final ClassElement element; |
19 final CompiledClass superclass; | 19 final FletchClassBuilder superclass; |
20 | 20 |
21 // The extra fields are synthetic fields not represented in any Dart source | 21 // The extra fields are synthetic fields not represented in any Dart source |
22 // code. They are used for the synthetic closure classes that are introduced | 22 // code. They are used for the synthetic closure classes that are introduced |
23 // behind the scenes. | 23 // behind the scenes. |
24 final int extraFields; | 24 final int extraFields; |
25 | 25 |
26 // TODO(kasperl): Hide these tables and go through a proper API to define | 26 // TODO(kasperl): Hide these tables and go through a proper API to define |
27 // and lookup methods. | 27 // and lookup methods. |
28 final Map<int, int> implicitAccessorTable = <int, int>{}; | 28 final Map<int, int> implicitAccessorTable = <int, int>{}; |
29 final Map<int, CompiledFunction> methodTable = <int, CompiledFunction>{}; | 29 final Map<int, FletchFunctionBuilder> methodTable = |
| 30 <int, FletchFunctionBuilder>{}; |
30 | 31 |
31 CompiledClass(this.id, this.element, this.superclass, {this.extraFields: 0}); | 32 FletchClassBuilder( |
| 33 this.id, this.element, this.superclass, {this.extraFields: 0}); |
32 | 34 |
33 /** | 35 /** |
34 * Returns the number of instance fields of all the super classes of this | 36 * Returns the number of instance fields of all the super classes of this |
35 * class. | 37 * class. |
36 * | 38 * |
37 * If this class has no super class (if it's Object), 0 is returned. | 39 * If this class has no super class (if it's Object), 0 is returned. |
38 */ | 40 */ |
39 int get superclassFields => hasSuperClass ? superclass.fields : 0; | 41 int get superclassFields => hasSuperClass ? superclass.fields : 0; |
40 | 42 |
41 bool get hasSuperClass => superclass != null; | 43 bool get hasSuperClass => superclass != null; |
42 | 44 |
43 int get fields { | 45 int get fields { |
44 int count = superclassFields + extraFields; | 46 int count = superclassFields + extraFields; |
45 if (element != null) { | 47 if (element != null) { |
46 // TODO(kasperl): Once we change compiled class to be immutable, we | 48 // TODO(kasperl): Once we change compiled class to be immutable, we |
47 // should cache the field count. | 49 // should cache the field count. |
48 element.implementation.forEachInstanceField((_, __) { count++; }); | 50 element.implementation.forEachInstanceField((_, __) { count++; }); |
49 } | 51 } |
50 return count; | 52 return count; |
51 } | 53 } |
52 | 54 |
53 void addToMethodTable(int selector, CompiledFunction compiledFunction) { | 55 void addToMethodTable(int selector, FletchFunctionBuilder functionBuilder) { |
54 methodTable[selector] = compiledFunction; | 56 methodTable[selector] = functionBuilder; |
55 } | 57 } |
56 | 58 |
57 // Add a selector for is-tests. The selector is only to be hit with the | 59 // Add a selector for is-tests. The selector is only to be hit with the |
58 // InvokeTest bytecode, as the function is not guraranteed to be valid. | 60 // InvokeTest bytecode, as the function is not guraranteed to be valid. |
59 void addIsSelector(int selector) { | 61 void addIsSelector(int selector) { |
60 // TODO(ajohnsen): 'null' is a placeholder. Generate dummy function? | 62 // TODO(ajohnsen): 'null' is a placeholder. Generate dummy function? |
61 methodTable[selector] = null; | 63 methodTable[selector] = null; |
62 } | 64 } |
63 | 65 |
64 // The method table for a class is a mapping from Fletch's integer | 66 // The method table for a class is a mapping from Fletch's integer |
65 // selectors to method ids. It contains all methods defined for a | 67 // selectors to method ids. It contains all methods defined for a |
66 // class including the implicit accessors. | 68 // class including the implicit accessors. |
67 Map<int, int> computeMethodTable(FletchBackend backend) { | 69 Map<int, int> computeMethodTable(FletchBackend backend) { |
68 Map<int, int> result = <int, int>{}; | 70 Map<int, int> result = <int, int>{}; |
69 List<int> selectors = implicitAccessorTable.keys.toList() | 71 List<int> selectors = implicitAccessorTable.keys.toList() |
70 ..addAll(methodTable.keys) | 72 ..addAll(methodTable.keys) |
71 ..sort(); | 73 ..sort(); |
72 for (int selector in selectors) { | 74 for (int selector in selectors) { |
73 if (methodTable.containsKey(selector)) { | 75 if (methodTable.containsKey(selector)) { |
74 CompiledFunction function = methodTable[selector]; | 76 FletchFunctionBuilder function = methodTable[selector]; |
75 result[selector] = function == null ? 0 : function.methodId; | 77 result[selector] = function == null ? 0 : function.methodId; |
76 } else { | 78 } else { |
77 result[selector] = implicitAccessorTable[selector]; | 79 result[selector] = implicitAccessorTable[selector]; |
78 } | 80 } |
79 } | 81 } |
80 return result; | 82 return result; |
81 } | 83 } |
82 | 84 |
83 void createImplicitAccessors(FletchBackend backend) { | 85 void createImplicitAccessors(FletchBackend backend) { |
84 implicitAccessorTable.clear(); | 86 implicitAccessorTable.clear(); |
(...skipping 15 matching lines...) Expand all Loading... |
100 } | 102 } |
101 | 103 |
102 fieldIndex++; | 104 fieldIndex++; |
103 }); | 105 }); |
104 } | 106 } |
105 | 107 |
106 void createIsEntries(FletchBackend backend) { | 108 void createIsEntries(FletchBackend backend) { |
107 if (element == null) return; | 109 if (element == null) return; |
108 | 110 |
109 Set superclasses = new Set(); | 111 Set superclasses = new Set(); |
110 for (CompiledClass current = superclass; | 112 for (FletchClassBuilder current = superclass; |
111 current != null; | 113 current != null; |
112 current = current.superclass) { | 114 current = current.superclass) { |
113 superclasses.add(current.element); | 115 superclasses.add(current.element); |
114 } | 116 } |
115 | 117 |
116 void createFor(ClassElement classElement) { | 118 void createFor(ClassElement classElement) { |
117 if (superclasses.contains(classElement)) return; | 119 if (superclasses.contains(classElement)) return; |
118 int fletchSelector = backend.context.toFletchIsSelector(classElement); | 120 int fletchSelector = backend.context.toFletchIsSelector(classElement); |
119 addIsSelector(fletchSelector); | 121 addIsSelector(fletchSelector); |
120 } | 122 } |
121 | 123 |
122 // Create for the current element. | 124 // Create for the current element. |
123 createFor(element); | 125 createFor(element); |
124 | 126 |
125 // Add all types related to 'implements'. | 127 // Add all types related to 'implements'. |
126 for (InterfaceType interfaceType in element.interfaces) { | 128 for (InterfaceType interfaceType in element.interfaces) { |
127 createFor(interfaceType.element); | 129 createFor(interfaceType.element); |
128 for (DartType type in interfaceType.element.allSupertypes) { | 130 for (DartType type in interfaceType.element.allSupertypes) { |
129 createFor(type.element); | 131 createFor(type.element); |
130 } | 132 } |
131 } | 133 } |
132 } | 134 } |
133 | 135 |
134 void createIsFunctionEntry(FletchBackend backend) { | 136 void createIsFunctionEntry(FletchBackend backend) { |
135 int fletchSelector = backend.context.toFletchIsSelector( | 137 int fletchSelector = backend.context.toFletchIsSelector( |
136 backend.compiler.functionClass); | 138 backend.compiler.functionClass); |
137 addIsSelector(fletchSelector); | 139 addIsSelector(fletchSelector); |
138 } | 140 } |
139 | 141 |
140 String toString() => "CompiledClass(${element.name}, $id)"; | 142 String toString() => "FletchClassBuilder(${element.name}, $id)"; |
141 } | 143 } |
OLD | NEW |