| 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 '../dart_types.dart'; | 7 import '../dart_types.dart'; |
| 8 import '../dart2jslib.dart'; | 8 import '../dart2jslib.dart'; |
| 9 import '../elements/elements.dart'; | 9 import '../elements/elements.dart'; |
| 10 import '../elements/modelx.dart'; | 10 import '../elements/modelx.dart'; |
| 11 import '../scanner/scannerlib.dart'; | 11 import '../scanner/scannerlib.dart'; |
| 12 import '../tree/tree.dart'; | 12 import '../tree/tree.dart'; |
| 13 import '../util/util.dart'; | 13 import '../util/util.dart'; |
| 14 | 14 |
| 15 // TODO(johnniwinther): Merge functionality with the `TreePrinter`. | 15 // TODO(johnniwinther): Merge functionality with the `TreePrinter`. |
| 16 class AstBuilder { | 16 class AstBuilder { |
| 17 final Token position; | 17 final Token position; |
| 18 | 18 |
| 19 AstBuilder(this.position); | 19 AstBuilder(this.position); |
| 20 | 20 |
| 21 int get charOffset => position.charOffset; | 21 int get charOffset => position.charOffset; |
| 22 | 22 |
| 23 final Modifiers finalModifiers = | 23 Modifiers modifiers({bool isConst: false, |
| 24 new Modifiers.withFlags(null, Modifiers.FLAG_FINAL); | 24 bool isFinal: false, |
| 25 final Modifiers constModifiers = | 25 bool isStatic: false}) { |
| 26 new Modifiers.withFlags(null, Modifiers.FLAG_CONST); | 26 List identifiers = []; |
| 27 final Modifiers staticConstModifiers = | 27 int flags = 0; |
| 28 new Modifiers.withFlags(null, | 28 if (isConst) { |
| 29 Modifiers.FLAG_STATIC | Modifiers.FLAG_CONST); | 29 identifiers.add(identifier('const')); |
| 30 flags |= Modifiers.FLAG_CONST; |
| 31 } |
| 32 if (isFinal) { |
| 33 identifiers.add(identifier('final')); |
| 34 flags |= Modifiers.FLAG_FINAL; |
| 35 } |
| 36 if (isStatic) { |
| 37 identifiers.add(identifier('static')); |
| 38 flags |= Modifiers.FLAG_STATIC; |
| 39 } |
| 40 return new Modifiers.withFlags( |
| 41 new NodeList(null, linkedList(identifiers), null, ''), |
| 42 flags); |
| 43 } |
| 30 | 44 |
| 31 Token keywordToken(String text) { | 45 Token keywordToken(String text) { |
| 32 return new KeywordToken(Keyword.keywords[text], position.charOffset); | 46 return new KeywordToken(Keyword.keywords[text], position.charOffset); |
| 33 } | 47 } |
| 34 | 48 |
| 35 Token stringToken(String text) { | 49 Token stringToken(String text) { |
| 36 return new StringToken.fromString(IDENTIFIER_INFO, text, charOffset); | 50 return new StringToken.fromString(IDENTIFIER_INFO, text, charOffset); |
| 37 } | 51 } |
| 38 | 52 |
| 39 Token symbolToken(PrecedenceInfo info) { | 53 Token symbolToken(PrecedenceInfo info) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 new Send(identifier('this'), identifier(fieldName)))); | 142 new Send(identifier('this'), identifier(fieldName)))); |
| 129 } | 143 } |
| 130 | 144 |
| 131 NewExpression newExpression(String typeName, | 145 NewExpression newExpression(String typeName, |
| 132 NodeList arguments, | 146 NodeList arguments, |
| 133 {bool isConst: false}) { | 147 {bool isConst: false}) { |
| 134 return new NewExpression(keywordToken(isConst ? 'const' : 'new'), | 148 return new NewExpression(keywordToken(isConst ? 'const' : 'new'), |
| 135 new Send(null, identifier(typeName), arguments)); | 149 new Send(null, identifier(typeName), arguments)); |
| 136 } | 150 } |
| 137 | 151 |
| 152 Send reference(Identifier identifier) { |
| 153 return new Send(null, identifier); |
| 154 } |
| 155 |
| 156 Send indexGet(Expression receiver, Expression index) { |
| 157 return new Send(receiver, |
| 158 new Operator(symbolToken(INDEX_INFO)), |
| 159 new NodeList.singleton(index)); |
| 160 } |
| 161 |
| 162 LiteralMapEntry mapLiteralEntry(Expression key, Expression value) { |
| 163 return new LiteralMapEntry(key, symbolToken(COLON_INFO), value); |
| 164 } |
| 165 |
| 166 LiteralMap mapLiteral(List<LiteralMapEntry> entries, {bool isConst: false}) { |
| 167 return new LiteralMap( |
| 168 null, // Type arguments. |
| 169 new NodeList(symbolToken(OPEN_CURLY_BRACKET_INFO), |
| 170 linkedList(entries), |
| 171 symbolToken(CLOSE_CURLY_BRACKET_INFO), |
| 172 ','), |
| 173 isConst ? keywordToken('const') : null); |
| 174 } |
| 138 } | 175 } |
| 139 | 176 |
| 140 class EnumCreator { | 177 class EnumCreator { |
| 141 final Compiler compiler; | 178 final Compiler compiler; |
| 142 final EnumClassElementX enumClass; | 179 final EnumClassElementX enumClass; |
| 143 | 180 |
| 144 EnumCreator(this.compiler, this.enumClass); | 181 EnumCreator(this.compiler, this.enumClass); |
| 145 | 182 |
| 146 void createMembers() { | 183 void createMembers() { |
| 147 Enum node = enumClass.node; | 184 Enum node = enumClass.node; |
| 148 InterfaceType enumType = enumClass.thisType; | 185 InterfaceType enumType = enumClass.thisType; |
| 149 AstBuilder builder = new AstBuilder(enumClass.position); | 186 AstBuilder builder = new AstBuilder(enumClass.position); |
| 150 | 187 |
| 151 InterfaceType intType = compiler.intClass.computeType(compiler); | 188 InterfaceType intType = compiler.intClass.computeType(compiler); |
| 152 InterfaceType stringType = compiler.stringClass.computeType(compiler); | 189 InterfaceType stringType = compiler.stringClass.computeType(compiler); |
| 153 | 190 |
| 154 EnumFieldElementX addInstanceMember(String name, InterfaceType type) { | 191 EnumFieldElementX addInstanceMember(String name, InterfaceType type) { |
| 155 Identifier identifier = builder.identifier(name); | 192 Identifier identifier = builder.identifier(name); |
| 156 VariableList variableList = new VariableList(builder.finalModifiers); | 193 VariableList variableList = |
| 194 new VariableList(builder.modifiers(isFinal: true)); |
| 157 variableList.type = type; | 195 variableList.type = type; |
| 158 EnumFieldElementX variable = new EnumFieldElementX( | 196 EnumFieldElementX variable = new EnumFieldElementX( |
| 159 identifier, enumClass, variableList, identifier); | 197 identifier, enumClass, variableList, identifier); |
| 160 enumClass.addMember(variable, compiler); | 198 enumClass.addMember(variable, compiler); |
| 161 return variable; | 199 return variable; |
| 162 } | 200 } |
| 163 | 201 |
| 164 EnumFieldElementX indexVariable = addInstanceMember('index', intType); | 202 EnumFieldElementX indexVariable = addInstanceMember('index', intType); |
| 165 EnumFieldElementX nameVariable = addInstanceMember('_name', stringType); | |
| 166 | 203 |
| 167 VariableDefinitions indexDefinition = builder.initializingFormal('index'); | 204 VariableDefinitions indexDefinition = builder.initializingFormal('index'); |
| 168 VariableDefinitions nameDefinition = builder.initializingFormal('_name'); | |
| 169 | 205 |
| 170 FunctionExpression constructorNode = builder.functionExpression( | 206 FunctionExpression constructorNode = builder.functionExpression( |
| 171 builder.constModifiers, | 207 builder.modifiers(isConst: true), |
| 172 enumClass.name, | 208 enumClass.name, |
| 173 builder.argumentList([indexDefinition, nameDefinition]), | 209 builder.argumentList([indexDefinition]), |
| 174 builder.emptyStatement()); | 210 builder.emptyStatement()); |
| 175 | 211 |
| 176 EnumConstructorElementX constructor = new EnumConstructorElementX( | 212 EnumConstructorElementX constructor = new EnumConstructorElementX( |
| 177 enumClass, | 213 enumClass, |
| 178 builder.constModifiers, | 214 builder.modifiers(isConst: true), |
| 179 constructorNode); | 215 constructorNode); |
| 180 | 216 |
| 181 EnumFormalElementX indexFormal = new EnumFormalElementX( | 217 EnumFormalElementX indexFormal = new EnumFormalElementX( |
| 182 constructor, | 218 constructor, |
| 183 indexDefinition, | 219 indexDefinition, |
| 184 builder.identifier('index'), | 220 builder.identifier('index'), |
| 185 indexVariable); | 221 indexVariable); |
| 186 | 222 |
| 187 EnumFormalElementX nameFormal = new EnumFormalElementX( | |
| 188 constructor, | |
| 189 nameDefinition, | |
| 190 builder.identifier('_name'), | |
| 191 nameVariable); | |
| 192 | |
| 193 FunctionSignatureX constructorSignature = new FunctionSignatureX( | 223 FunctionSignatureX constructorSignature = new FunctionSignatureX( |
| 194 requiredParameters: builder.linkedList([indexFormal, nameFormal]), | 224 requiredParameters: builder.linkedList([indexFormal]), |
| 195 requiredParameterCount: 2, | 225 requiredParameterCount: 1, |
| 196 type: new FunctionType(constructor, const VoidType(), | 226 type: new FunctionType(constructor, const VoidType(), |
| 197 <DartType>[intType, stringType])); | 227 <DartType>[intType])); |
| 198 constructor.functionSignatureCache = constructorSignature; | 228 constructor.functionSignatureCache = constructorSignature; |
| 199 enumClass.addMember(constructor, compiler); | 229 enumClass.addMember(constructor, compiler); |
| 200 | 230 |
| 201 VariableList variableList = new VariableList(builder.staticConstModifiers); | 231 VariableList variableList = |
| 232 new VariableList(builder.modifiers(isStatic: true, isConst: true)); |
| 202 variableList.type = enumType; | 233 variableList.type = enumType; |
| 203 int index = 0; | 234 int index = 0; |
| 204 List<Node> valueReferences = <Node>[]; | 235 List<Node> valueReferences = <Node>[]; |
| 236 List<LiteralMapEntry> mapEntries = <LiteralMapEntry>[]; |
| 205 for (Link<Node> link = node.names.nodes; | 237 for (Link<Node> link = node.names.nodes; |
| 206 !link.isEmpty; | 238 !link.isEmpty; |
| 207 link = link.tail) { | 239 link = link.tail) { |
| 208 Identifier name = link.head; | 240 Identifier name = link.head; |
| 209 AstBuilder valueBuilder = new AstBuilder(name.token); | 241 AstBuilder valueBuilder = new AstBuilder(name.token); |
| 210 valueReferences.add(new Send(null, name)); | 242 |
| 243 // Add reference for the `values` field. |
| 244 valueReferences.add(valueBuilder.reference(name)); |
| 245 |
| 246 // Add map entry for `toString` implementation. |
| 247 mapEntries.add(valueBuilder.mapLiteralEntry( |
| 248 valueBuilder.literalInt(index), |
| 249 valueBuilder.literalString('${enumClass.name}.${name.source}'))); |
| 211 | 250 |
| 212 Expression initializer = valueBuilder.newExpression( | 251 Expression initializer = valueBuilder.newExpression( |
| 213 enumClass.name, | 252 enumClass.name, |
| 214 valueBuilder.argumentList([ | 253 valueBuilder.argumentList([valueBuilder.literalInt(index)]), |
| 215 valueBuilder.literalInt(index), | |
| 216 valueBuilder.literalString('${name.source}') | |
| 217 ]), | |
| 218 isConst: true); | 254 isConst: true); |
| 219 SendSet definition = valueBuilder.createDefinition(name, initializer); | 255 SendSet definition = valueBuilder.createDefinition(name, initializer); |
| 220 | 256 |
| 221 EnumFieldElementX field = new EnumFieldElementX( | 257 EnumFieldElementX field = new EnumFieldElementX( |
| 222 name, enumClass, variableList, definition, initializer); | 258 name, enumClass, variableList, definition, initializer); |
| 223 enumClass.addMember(field, compiler); | 259 enumClass.addMember(field, compiler); |
| 224 index++; | 260 index++; |
| 225 } | 261 } |
| 226 | 262 |
| 227 VariableList valuesVariableList = | 263 VariableList valuesVariableList = |
| 228 new VariableList(builder.staticConstModifiers); | 264 new VariableList(builder.modifiers(isStatic: true, isConst: true)); |
| 229 InterfaceType listType = compiler.listClass.computeType(compiler); | 265 InterfaceType listType = compiler.listClass.computeType(compiler); |
| 230 valuesVariableList.type = listType.createInstantiation([enumType]); | 266 valuesVariableList.type = listType.createInstantiation([enumType]); |
| 231 | 267 |
| 232 Identifier valuesIdentifier = builder.identifier('values'); | 268 Identifier valuesIdentifier = builder.identifier('values'); |
| 233 // TODO(johnniwinther): Add type argument. | 269 // TODO(johnniwinther): Add type argument. |
| 234 Expression initializer = builder.listLiteral( | 270 Expression initializer = builder.listLiteral( |
| 235 valueReferences, isConst: true); | 271 valueReferences, isConst: true); |
| 236 | 272 |
| 237 Node definition = builder.createDefinition(valuesIdentifier, initializer); | 273 Node definition = builder.createDefinition(valuesIdentifier, initializer); |
| 238 | 274 |
| 239 EnumFieldElementX valuesVariable = new EnumFieldElementX( | 275 EnumFieldElementX valuesVariable = new EnumFieldElementX( |
| 240 valuesIdentifier, enumClass, valuesVariableList, | 276 valuesIdentifier, enumClass, valuesVariableList, |
| 241 definition, initializer); | 277 definition, initializer); |
| 242 | 278 |
| 243 enumClass.addMember(valuesVariable, compiler); | 279 enumClass.addMember(valuesVariable, compiler); |
| 244 | 280 |
| 245 // TODO(johnniwinther): Support return type. Note `String` might be prefixed | 281 // TODO(johnniwinther): Support return type. Note `String` might be prefixed |
| 246 // or not imported within the current library. | 282 // or not imported within the current library. |
| 247 FunctionExpression toStringNode = builder.functionExpression( | 283 FunctionExpression toStringNode = builder.functionExpression( |
| 248 Modifiers.EMPTY, | 284 Modifiers.EMPTY, |
| 249 'toString', | 285 'toString', |
| 250 builder.argumentList([]), | 286 builder.argumentList([]), |
| 251 builder.returnStatement( | 287 builder.returnStatement( |
| 252 new StringInterpolation( | 288 builder.indexGet( |
| 253 builder.literalString('${enumClass.name}.', suffix: ''), | 289 builder.mapLiteral(mapEntries, isConst: true), |
| 254 new NodeList.singleton(new StringInterpolationPart( | 290 builder.reference(builder.identifier('index'))) |
| 255 new Send(null, builder.identifier('_name')), | 291 ) |
| 256 builder.literalString('', prefix: ''))) | |
| 257 )) | |
| 258 ); | 292 ); |
| 259 | 293 |
| 260 EnumMethodElementX toString = new EnumMethodElementX('toString', | 294 EnumMethodElementX toString = new EnumMethodElementX('toString', |
| 261 enumClass, Modifiers.EMPTY, toStringNode); | 295 enumClass, Modifiers.EMPTY, toStringNode); |
| 262 FunctionSignatureX toStringSignature = new FunctionSignatureX( | 296 FunctionSignatureX toStringSignature = new FunctionSignatureX( |
| 263 type: new FunctionType(toString, stringType)); | 297 type: new FunctionType(toString, stringType)); |
| 264 toString.functionSignatureCache = toStringSignature; | 298 toString.functionSignatureCache = toStringSignature; |
| 265 enumClass.addMember(toString, compiler); | 299 enumClass.addMember(toString, compiler); |
| 266 } | 300 } |
| 267 } | 301 } |
| OLD | NEW |