OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.resolution.enum_creator; | 5 library dart2js.resolution.enum_creator; |
6 | 6 |
7 import '../compiler.dart'; | 7 import '../core_types.dart' show |
8 import '../core_types.dart'; | 8 CoreTypes; |
9 import '../dart_types.dart'; | 9 import '../dart_types.dart'; |
| 10 import '../diagnostics/diagnostic_listener.dart' show |
| 11 DiagnosticReporter; |
10 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
11 import '../elements/modelx.dart'; | 13 import '../elements/modelx.dart'; |
12 import '../tokens/keyword.dart' show | 14 import '../tokens/keyword.dart' show |
13 Keyword; | 15 Keyword; |
14 import '../tokens/precedence.dart'; | 16 import '../tokens/precedence.dart'; |
15 import '../tokens/precedence_constants.dart' as Precedence; | 17 import '../tokens/precedence_constants.dart' as Precedence; |
16 import '../tokens/token.dart'; | 18 import '../tokens/token.dart'; |
17 import '../tree/tree.dart'; | 19 import '../tree/tree.dart'; |
18 import '../util/util.dart'; | 20 import '../util/util.dart'; |
19 | 21 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 null, // Type arguments. | 175 null, // Type arguments. |
174 new NodeList(symbolToken(Precedence.OPEN_CURLY_BRACKET_INFO), | 176 new NodeList(symbolToken(Precedence.OPEN_CURLY_BRACKET_INFO), |
175 linkedList(entries), | 177 linkedList(entries), |
176 symbolToken(Precedence.CLOSE_CURLY_BRACKET_INFO), | 178 symbolToken(Precedence.CLOSE_CURLY_BRACKET_INFO), |
177 ','), | 179 ','), |
178 isConst ? keywordToken('const') : null); | 180 isConst ? keywordToken('const') : null); |
179 } | 181 } |
180 } | 182 } |
181 | 183 |
182 class EnumCreator { | 184 class EnumCreator { |
183 final Compiler compiler; | 185 final DiagnosticReporter reporter; |
| 186 final CoreTypes coreTypes; |
184 final EnumClassElementX enumClass; | 187 final EnumClassElementX enumClass; |
185 | 188 |
186 EnumCreator(this.compiler, this.enumClass); | 189 EnumCreator(this.reporter, this.coreTypes, this.enumClass); |
187 | |
188 CoreTypes get coreTypes => compiler.coreTypes; | |
189 | 190 |
190 void createMembers() { | 191 void createMembers() { |
191 Enum node = enumClass.node; | 192 Enum node = enumClass.node; |
192 InterfaceType enumType = enumClass.thisType; | 193 InterfaceType enumType = enumClass.thisType; |
193 AstBuilder builder = new AstBuilder(enumClass.position.charOffset); | 194 AstBuilder builder = new AstBuilder(enumClass.position.charOffset); |
194 | 195 |
195 InterfaceType intType = coreTypes.intType; | 196 InterfaceType intType = coreTypes.intType; |
196 InterfaceType stringType = coreTypes.stringType; | 197 InterfaceType stringType = coreTypes.stringType; |
197 | 198 |
198 EnumFieldElementX addInstanceMember(String name, InterfaceType type) { | 199 EnumFieldElementX addInstanceMember(String name, InterfaceType type) { |
199 Identifier identifier = builder.identifier(name); | 200 Identifier identifier = builder.identifier(name); |
200 VariableList variableList = | 201 VariableList variableList = |
201 new VariableList(builder.modifiers(isFinal: true)); | 202 new VariableList(builder.modifiers(isFinal: true)); |
202 variableList.type = type; | 203 variableList.type = type; |
203 EnumFieldElementX variable = new EnumFieldElementX( | 204 EnumFieldElementX variable = new EnumFieldElementX( |
204 identifier, enumClass, variableList, identifier); | 205 identifier, enumClass, variableList, identifier); |
205 enumClass.addMember(variable, compiler); | 206 enumClass.addMember(variable, reporter); |
206 return variable; | 207 return variable; |
207 } | 208 } |
208 | 209 |
209 EnumFieldElementX indexVariable = addInstanceMember('index', intType); | 210 EnumFieldElementX indexVariable = addInstanceMember('index', intType); |
210 | 211 |
211 VariableDefinitions indexDefinition = builder.initializingFormal('index'); | 212 VariableDefinitions indexDefinition = builder.initializingFormal('index'); |
212 | 213 |
213 FunctionExpression constructorNode = builder.functionExpression( | 214 FunctionExpression constructorNode = builder.functionExpression( |
214 builder.modifiers(isConst: true), | 215 builder.modifiers(isConst: true), |
215 enumClass.name, | 216 enumClass.name, |
(...skipping 10 matching lines...) Expand all Loading... |
226 indexDefinition, | 227 indexDefinition, |
227 builder.identifier('index'), | 228 builder.identifier('index'), |
228 indexVariable); | 229 indexVariable); |
229 | 230 |
230 FunctionSignatureX constructorSignature = new FunctionSignatureX( | 231 FunctionSignatureX constructorSignature = new FunctionSignatureX( |
231 requiredParameters: [indexFormal], | 232 requiredParameters: [indexFormal], |
232 requiredParameterCount: 1, | 233 requiredParameterCount: 1, |
233 type: new FunctionType(constructor, const VoidType(), | 234 type: new FunctionType(constructor, const VoidType(), |
234 <DartType>[intType])); | 235 <DartType>[intType])); |
235 constructor.functionSignatureCache = constructorSignature; | 236 constructor.functionSignatureCache = constructorSignature; |
236 enumClass.addMember(constructor, compiler); | 237 enumClass.addMember(constructor, reporter); |
237 | 238 |
238 List<FieldElement> enumValues = <FieldElement>[]; | 239 List<FieldElement> enumValues = <FieldElement>[]; |
239 VariableList variableList = | 240 VariableList variableList = |
240 new VariableList(builder.modifiers(isStatic: true, isConst: true)); | 241 new VariableList(builder.modifiers(isStatic: true, isConst: true)); |
241 variableList.type = enumType; | 242 variableList.type = enumType; |
242 int index = 0; | 243 int index = 0; |
243 List<Node> valueReferences = <Node>[]; | 244 List<Node> valueReferences = <Node>[]; |
244 List<LiteralMapEntry> mapEntries = <LiteralMapEntry>[]; | 245 List<LiteralMapEntry> mapEntries = <LiteralMapEntry>[]; |
245 for (Link<Node> link = node.names.nodes; | 246 for (Link<Node> link = node.names.nodes; |
246 !link.isEmpty; | 247 !link.isEmpty; |
(...skipping 11 matching lines...) Expand all Loading... |
258 | 259 |
259 Expression initializer = valueBuilder.newExpression( | 260 Expression initializer = valueBuilder.newExpression( |
260 enumClass.name, | 261 enumClass.name, |
261 valueBuilder.argumentList([valueBuilder.literalInt(index)]), | 262 valueBuilder.argumentList([valueBuilder.literalInt(index)]), |
262 isConst: true); | 263 isConst: true); |
263 SendSet definition = valueBuilder.createDefinition(name, initializer); | 264 SendSet definition = valueBuilder.createDefinition(name, initializer); |
264 | 265 |
265 EnumFieldElementX field = new EnumFieldElementX( | 266 EnumFieldElementX field = new EnumFieldElementX( |
266 name, enumClass, variableList, definition, initializer); | 267 name, enumClass, variableList, definition, initializer); |
267 enumValues.add(field); | 268 enumValues.add(field); |
268 enumClass.addMember(field, compiler); | 269 enumClass.addMember(field, reporter); |
269 index++; | 270 index++; |
270 } | 271 } |
271 | 272 |
272 VariableList valuesVariableList = | 273 VariableList valuesVariableList = |
273 new VariableList(builder.modifiers(isStatic: true, isConst: true)); | 274 new VariableList(builder.modifiers(isStatic: true, isConst: true)); |
274 valuesVariableList.type = coreTypes.listType(enumType); | 275 valuesVariableList.type = coreTypes.listType(enumType); |
275 | 276 |
276 Identifier valuesIdentifier = builder.identifier('values'); | 277 Identifier valuesIdentifier = builder.identifier('values'); |
277 // TODO(johnniwinther): Add type argument. | 278 // TODO(johnniwinther): Add type argument. |
278 Expression initializer = builder.listLiteral( | 279 Expression initializer = builder.listLiteral( |
279 valueReferences, isConst: true); | 280 valueReferences, isConst: true); |
280 | 281 |
281 Node definition = builder.createDefinition(valuesIdentifier, initializer); | 282 Node definition = builder.createDefinition(valuesIdentifier, initializer); |
282 | 283 |
283 EnumFieldElementX valuesVariable = new EnumFieldElementX( | 284 EnumFieldElementX valuesVariable = new EnumFieldElementX( |
284 valuesIdentifier, enumClass, valuesVariableList, | 285 valuesIdentifier, enumClass, valuesVariableList, |
285 definition, initializer); | 286 definition, initializer); |
286 | 287 |
287 enumClass.addMember(valuesVariable, compiler); | 288 enumClass.addMember(valuesVariable, reporter); |
288 | 289 |
289 // TODO(johnniwinther): Support return type. Note `String` might be prefixed | 290 // TODO(johnniwinther): Support return type. Note `String` might be prefixed |
290 // or not imported within the current library. | 291 // or not imported within the current library. |
291 FunctionExpression toStringNode = builder.functionExpression( | 292 FunctionExpression toStringNode = builder.functionExpression( |
292 Modifiers.EMPTY, | 293 Modifiers.EMPTY, |
293 'toString', | 294 'toString', |
294 builder.argumentList([]), | 295 builder.argumentList([]), |
295 builder.returnStatement( | 296 builder.returnStatement( |
296 builder.indexGet( | 297 builder.indexGet( |
297 builder.mapLiteral(mapEntries, isConst: true), | 298 builder.mapLiteral(mapEntries, isConst: true), |
298 builder.reference(builder.identifier('index'))) | 299 builder.reference(builder.identifier('index'))) |
299 ) | 300 ) |
300 ); | 301 ); |
301 | 302 |
302 EnumMethodElementX toString = new EnumMethodElementX('toString', | 303 EnumMethodElementX toString = new EnumMethodElementX('toString', |
303 enumClass, Modifiers.EMPTY, toStringNode); | 304 enumClass, Modifiers.EMPTY, toStringNode); |
304 FunctionSignatureX toStringSignature = new FunctionSignatureX( | 305 FunctionSignatureX toStringSignature = new FunctionSignatureX( |
305 type: new FunctionType(toString, stringType)); | 306 type: new FunctionType(toString, stringType)); |
306 toString.functionSignatureCache = toStringSignature; | 307 toString.functionSignatureCache = toStringSignature; |
307 enumClass.addMember(toString, compiler); | 308 enumClass.addMember(toString, reporter); |
308 | 309 |
309 enumClass.enumValues = enumValues; | 310 enumClass.enumValues = enumValues; |
310 } | 311 } |
311 } | 312 } |
OLD | NEW |