OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE.md file. | |
4 | |
5 library fletchc.compiled_class; | |
6 | |
7 import 'package:compiler/src/dart_types.dart'; | |
8 import 'package:compiler/src/elements/elements.dart'; | |
9 import 'package:compiler/src/universe/universe.dart'; | |
10 | |
11 import 'compiled_function.dart' show | |
12 CompiledFunction; | |
13 | |
14 import 'fletch_backend.dart'; | |
15 | |
16 class CompiledClass { | |
17 final int id; | |
18 final ClassElement element; | |
19 final CompiledClass superclass; | |
20 | |
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 | |
23 // behind the scenes. | |
24 final int extraFields; | |
25 | |
26 // TODO(kasperl): Hide these tables and go through a proper API to define | |
27 // and lookup methods. | |
28 final Map<int, int> implicitAccessorTable = <int, int>{}; | |
29 final Map<int, CompiledFunction> methodTable = <int, CompiledFunction>{}; | |
30 | |
31 CompiledClass(this.id, this.element, this.superclass, {this.extraFields: 0}); | |
32 | |
33 /** | |
34 * Returns the number of instance fields of all the super classes of this | |
35 * class. | |
36 * | |
37 * If this class has no super class (if it's Object), 0 is returned. | |
38 */ | |
39 int get superclassFields => hasSuperClass ? superclass.fields : 0; | |
40 | |
41 bool get hasSuperClass => superclass != null; | |
42 | |
43 int get fields { | |
44 int count = superclassFields + extraFields; | |
45 if (element != null) { | |
46 // TODO(kasperl): Once we change compiled class to be immutable, we | |
47 // should cache the field count. | |
48 element.implementation.forEachInstanceField((_, __) { count++; }); | |
49 } | |
50 return count; | |
51 } | |
52 | |
53 void addToMethodTable(int selector, CompiledFunction compiledFunction) { | |
54 methodTable[selector] = compiledFunction; | |
55 } | |
56 | |
57 // 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. | |
59 void addIsSelector(int selector) { | |
60 // TODO(ajohnsen): 'null' is a placeholder. Generate dummy function? | |
61 methodTable[selector] = null; | |
62 } | |
63 | |
64 // 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 | |
66 // class including the implicit accessors. | |
67 Map<int, int> computeMethodTable(FletchBackend backend) { | |
68 Map<int, int> result = <int, int>{}; | |
69 List<int> selectors = implicitAccessorTable.keys.toList() | |
70 ..addAll(methodTable.keys) | |
71 ..sort(); | |
72 for (int selector in selectors) { | |
73 if (methodTable.containsKey(selector)) { | |
74 CompiledFunction function = methodTable[selector]; | |
75 result[selector] = function == null ? 0 : function.methodId; | |
76 } else { | |
77 result[selector] = implicitAccessorTable[selector]; | |
78 } | |
79 } | |
80 return result; | |
81 } | |
82 | |
83 void createImplicitAccessors(FletchBackend backend) { | |
84 implicitAccessorTable.clear(); | |
85 // If we don't have an element (stub class), we don't have anything to | |
86 // generate accessors for. | |
87 if (element == null) return; | |
88 // TODO(ajohnsen): Don't do this once dart2js can enqueue field getters in | |
89 // CodegenEnqueuer. | |
90 int fieldIndex = superclassFields; | |
91 element.implementation.forEachInstanceField((enclosing, field) { | |
92 var getter = new Selector.getter(field.name, field.library); | |
93 int getterSelector = backend.context.toFletchSelector(getter); | |
94 implicitAccessorTable[getterSelector] = backend.makeGetter(fieldIndex); | |
95 | |
96 if (!field.isFinal) { | |
97 var setter = new Selector.setter(field.name, field.library); | |
98 var setterSelector = backend.context.toFletchSelector(setter); | |
99 implicitAccessorTable[setterSelector] = backend.makeSetter(fieldIndex); | |
100 } | |
101 | |
102 fieldIndex++; | |
103 }); | |
104 } | |
105 | |
106 void createIsEntries(FletchBackend backend) { | |
107 if (element == null) return; | |
108 | |
109 Set superclasses = new Set(); | |
110 for (CompiledClass current = superclass; | |
111 current != null; | |
112 current = current.superclass) { | |
113 superclasses.add(current.element); | |
114 } | |
115 | |
116 void createFor(ClassElement classElement) { | |
117 if (superclasses.contains(classElement)) return; | |
118 int fletchSelector = backend.context.toFletchIsSelector(classElement); | |
119 addIsSelector(fletchSelector); | |
120 } | |
121 | |
122 // Create for the current element. | |
123 createFor(element); | |
124 | |
125 // Add all types related to 'implements'. | |
126 for (InterfaceType interfaceType in element.interfaces) { | |
127 createFor(interfaceType.element); | |
128 for (DartType type in interfaceType.element.allSupertypes) { | |
129 createFor(type.element); | |
130 } | |
131 } | |
132 } | |
133 | |
134 void createIsFunctionEntry(FletchBackend backend) { | |
135 int fletchSelector = backend.context.toFletchIsSelector( | |
136 backend.compiler.functionClass); | |
137 addIsSelector(fletchSelector); | |
138 } | |
139 | |
140 String toString() => "CompiledClass(${element.name}, $id)"; | |
141 } | |
OLD | NEW |