OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.js_emitter.full_emitter; | 5 part of dart2js.js_emitter.full_emitter; |
6 | 6 |
7 /// This class should morph into something that makes it easy to build | 7 /// This class should morph into something that makes it easy to build |
8 /// JavaScript representations of libraries, class-sides, and instance-sides. | 8 /// JavaScript representations of libraries, class-sides, and instance-sides. |
9 /// Initially, it is just a placeholder for code that is moved from | 9 /// Initially, it is just a placeholder for code that is moved from |
10 /// [CodeEmitterTask]. | 10 /// [CodeEmitterTask]. |
11 class ContainerBuilder extends CodeEmitterHelper { | 11 class ContainerBuilder extends CodeEmitterHelper { |
12 | |
13 void addMemberMethod(DartMethod method, ClassBuilder builder) { | 12 void addMemberMethod(DartMethod method, ClassBuilder builder) { |
14 MethodElement member = method.element; | 13 MethodElement member = method.element; |
15 jsAst.Name name = method.name; | 14 jsAst.Name name = method.name; |
16 FunctionSignature parameters = member.functionSignature; | 15 FunctionSignature parameters = member.functionSignature; |
17 jsAst.Expression code = method.code; | 16 jsAst.Expression code = method.code; |
18 bool needsStubs = method.parameterStubs.isNotEmpty; | 17 bool needsStubs = method.parameterStubs.isNotEmpty; |
19 bool canBeApplied = method.canBeApplied; | 18 bool canBeApplied = method.canBeApplied; |
20 bool canBeReflected = method.canBeReflected; | 19 bool canBeReflected = method.canBeReflected; |
21 bool canTearOff = method.needsTearOff; | 20 bool canTearOff = method.needsTearOff; |
22 jsAst.Name tearOffName = method.tearOffName; | 21 jsAst.Name tearOffName = method.tearOffName; |
23 bool isClosure = method is InstanceMethod && method.isClosureCallMethod; | 22 bool isClosure = method is InstanceMethod && method.isClosureCallMethod; |
24 jsAst.Name superAlias = method is InstanceMethod ? method.aliasName : null; | 23 jsAst.Name superAlias = method is InstanceMethod ? method.aliasName : null; |
25 bool hasSuperAlias = superAlias != null; | 24 bool hasSuperAlias = superAlias != null; |
26 jsAst.Expression memberTypeExpression = method.functionType; | 25 jsAst.Expression memberTypeExpression = method.functionType; |
27 | 26 |
28 bool needStructuredInfo = | 27 bool needStructuredInfo = |
29 canTearOff || canBeReflected || canBeApplied || hasSuperAlias; | 28 canTearOff || canBeReflected || canBeApplied || hasSuperAlias; |
30 | 29 |
31 emitter.interceptorEmitter.recordMangledNameOfMemberMethod(member, name); | 30 emitter.interceptorEmitter.recordMangledNameOfMemberMethod(member, name); |
32 | 31 |
33 if (!needStructuredInfo) { | 32 if (!needStructuredInfo) { |
34 compiler.dumpInfoTask.registerElementAst(member, | 33 compiler.dumpInfoTask |
35 builder.addProperty(name, code)); | 34 .registerElementAst(member, builder.addProperty(name, code)); |
36 | 35 |
37 for (ParameterStubMethod stub in method.parameterStubs) { | 36 for (ParameterStubMethod stub in method.parameterStubs) { |
38 assert(stub.callName == null); | 37 assert(stub.callName == null); |
39 jsAst.Property property = builder.addProperty(stub.name, stub.code); | 38 jsAst.Property property = builder.addProperty(stub.name, stub.code); |
40 compiler.dumpInfoTask.registerElementAst(member, property); | 39 compiler.dumpInfoTask.registerElementAst(member, property); |
41 emitter.interceptorEmitter | 40 emitter.interceptorEmitter |
42 .recordMangledNameOfMemberMethod(member, stub.name); | 41 .recordMangledNameOfMemberMethod(member, stub.name); |
43 } | 42 } |
44 return; | 43 return; |
45 } | 44 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 expressions.add(js.quoteName(superAlias)); | 79 expressions.add(js.quoteName(superAlias)); |
81 } | 80 } |
82 | 81 |
83 expressions.add(code); | 82 expressions.add(code); |
84 | 83 |
85 bool onlyNeedsSuperAlias = | 84 bool onlyNeedsSuperAlias = |
86 !(canTearOff || canBeReflected || canBeApplied || needsStubs); | 85 !(canTearOff || canBeReflected || canBeApplied || needsStubs); |
87 | 86 |
88 if (onlyNeedsSuperAlias) { | 87 if (onlyNeedsSuperAlias) { |
89 jsAst.ArrayInitializer arrayInit = | 88 jsAst.ArrayInitializer arrayInit = |
90 new jsAst.ArrayInitializer(expressions); | 89 new jsAst.ArrayInitializer(expressions); |
91 compiler.dumpInfoTask.registerElementAst(member, | 90 compiler.dumpInfoTask |
92 builder.addProperty(name, arrayInit)); | 91 .registerElementAst(member, builder.addProperty(name, arrayInit)); |
93 return; | 92 return; |
94 } | 93 } |
95 | 94 |
96 jsAst.Literal callSelectorString; | 95 jsAst.Literal callSelectorString; |
97 if (method.callName == null) { | 96 if (method.callName == null) { |
98 callSelectorString = new jsAst.LiteralNull(); | 97 callSelectorString = new jsAst.LiteralNull(); |
99 } else { | 98 } else { |
100 callSelectorString = js.quoteName(method.callName); | 99 callSelectorString = js.quoteName(method.callName); |
101 } | 100 } |
102 | 101 |
(...skipping 16 matching lines...) Expand all Loading... |
119 if (member.isInstanceMember) { | 118 if (member.isInstanceMember) { |
120 expressions.add(js.quoteName(invocationName)); | 119 expressions.add(js.quoteName(invocationName)); |
121 } | 120 } |
122 jsAst.Name callName = stub.callName; | 121 jsAst.Name callName = stub.callName; |
123 jsAst.Literal callSelectorString = | 122 jsAst.Literal callSelectorString = |
124 (callName == null) ? new jsAst.LiteralNull() : js.quoteName(callName); | 123 (callName == null) ? new jsAst.LiteralNull() : js.quoteName(callName); |
125 tearOffInfo.add(callSelectorString); | 124 tearOffInfo.add(callSelectorString); |
126 } | 125 } |
127 | 126 |
128 expressions | 127 expressions |
129 ..addAll(tearOffInfo) | 128 ..addAll(tearOffInfo) |
130 ..add((tearOffName == null || member.isAccessor) | 129 ..add((tearOffName == null || member.isAccessor) |
131 ? js("null") : js.quoteName(tearOffName)) | 130 ? js("null") |
132 ..add(js.number(requiredParameterCount)) | 131 : js.quoteName(tearOffName)) |
133 ..add(js.number(optionalParameterCount)) | 132 ..add(js.number(requiredParameterCount)) |
134 ..add(memberTypeExpression == null ? js("null") : memberTypeExpression) | 133 ..add(js.number(optionalParameterCount)) |
135 ..addAll(task.metadataCollector.reifyDefaultArguments(member)); | 134 ..add(memberTypeExpression == null ? js("null") : memberTypeExpression) |
| 135 ..addAll(task.metadataCollector.reifyDefaultArguments(member)); |
136 | 136 |
137 if (canBeReflected || canBeApplied) { | 137 if (canBeReflected || canBeApplied) { |
138 parameters.forEachParameter((Element parameter) { | 138 parameters.forEachParameter((Element parameter) { |
139 expressions.add(task.metadataCollector.reifyName(parameter.name)); | 139 expressions.add(task.metadataCollector.reifyName(parameter.name)); |
140 if (backend.mustRetainMetadata) { | 140 if (backend.mustRetainMetadata) { |
141 Iterable<jsAst.Expression> metadataIndices = | 141 Iterable<jsAst.Expression> metadataIndices = |
142 parameter.metadata.map((MetadataAnnotation annotation) { | 142 parameter.metadata.map((MetadataAnnotation annotation) { |
143 ConstantValue constant = | 143 ConstantValue constant = |
144 backend.constants.getConstantValueForMetadata(annotation); | 144 backend.constants.getConstantValueForMetadata(annotation); |
145 backend.constants.addCompileTimeConstantForEmission(constant); | 145 backend.constants.addCompileTimeConstantForEmission(constant); |
146 return task.metadataCollector.reifyMetadata(annotation); | 146 return task.metadataCollector.reifyMetadata(annotation); |
147 }); | 147 }); |
148 expressions.add(new jsAst.ArrayInitializer(metadataIndices.toList())); | 148 expressions.add(new jsAst.ArrayInitializer(metadataIndices.toList())); |
149 } | 149 } |
150 }); | 150 }); |
151 } | 151 } |
152 if (canBeReflected) { | 152 if (canBeReflected) { |
153 jsAst.LiteralString reflectionName; | 153 jsAst.LiteralString reflectionName; |
154 if (member.isConstructor) { | 154 if (member.isConstructor) { |
155 // TODO(herhut): This registers name as a mangled name. Do we need this | 155 // TODO(herhut): This registers name as a mangled name. Do we need this |
156 // given that we use a different name below? | 156 // given that we use a different name below? |
157 emitter.getReflectionName(member, name); | 157 emitter.getReflectionName(member, name); |
158 reflectionName = | 158 reflectionName = new jsAst.LiteralString( |
159 new jsAst.LiteralString( | 159 '"new ${Elements.reconstructConstructorName(member)}"'); |
160 '"new ${Elements.reconstructConstructorName(member)}"'); | |
161 } else { | 160 } else { |
162 reflectionName = | 161 reflectionName = js.string(namer.privateName(member.memberName)); |
163 js.string(namer.privateName(member.memberName)); | |
164 } | 162 } |
165 expressions | 163 expressions |
166 ..add(reflectionName) | 164 ..add(reflectionName) |
167 ..addAll(task.metadataCollector.computeMetadata(member)); | 165 ..addAll(task.metadataCollector.computeMetadata(member)); |
168 } else if (isClosure && canBeApplied) { | 166 } else if (isClosure && canBeApplied) { |
169 expressions.add(js.string(namer.privateName(member.memberName))); | 167 expressions.add(js.string(namer.privateName(member.memberName))); |
170 } | 168 } |
171 | 169 |
172 jsAst.ArrayInitializer arrayInit = | 170 jsAst.ArrayInitializer arrayInit = |
173 new jsAst.ArrayInitializer(expressions.toList()); | 171 new jsAst.ArrayInitializer(expressions.toList()); |
174 compiler.dumpInfoTask.registerElementAst(member, | 172 compiler.dumpInfoTask |
175 builder.addProperty(name, arrayInit)); | 173 .registerElementAst(member, builder.addProperty(name, arrayInit)); |
176 } | 174 } |
177 | 175 |
178 void addMemberField(Field field, ClassBuilder builder) { | 176 void addMemberField(Field field, ClassBuilder builder) { |
179 // For now, do nothing. | 177 // For now, do nothing. |
180 } | 178 } |
181 } | 179 } |
OLD | NEW |