Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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; | 5 part of dart2js; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * The [ConstantHandler] keeps track of compile-time constants, | 8 * The [ConstantHandler] keeps track of compile-time constants, |
| 9 * initializations of global and static fields, and default values of | 9 * initializations of global and static fields, and default values of |
| 10 * optional parameters. | 10 * optional parameters. |
| (...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 handler.registerInstantiatedType(type, elements); | 379 handler.registerInstantiatedType(type, elements); |
| 380 Constant constant = new ListConstant(type, arguments); | 380 Constant constant = new ListConstant(type, arguments); |
| 381 handler.registerCompileTimeConstant(constant, elements); | 381 handler.registerCompileTimeConstant(constant, elements); |
| 382 return constant; | 382 return constant; |
| 383 } | 383 } |
| 384 | 384 |
| 385 Constant visitLiteralMap(LiteralMap node) { | 385 Constant visitLiteralMap(LiteralMap node) { |
| 386 if (!node.isConst()) { | 386 if (!node.isConst()) { |
| 387 return signalNotCompileTimeConstant(node); | 387 return signalNotCompileTimeConstant(node); |
| 388 } | 388 } |
| 389 List<StringConstant> keys = <StringConstant>[]; | 389 List<Constant> keys = <Constant>[]; |
| 390 Map<StringConstant, Constant> map = new Map<StringConstant, Constant>(); | 390 Map<Constant, Constant> map = new Map<Constant, Constant>(); |
| 391 for (Link<Node> link = node.entries.nodes; | 391 for (Link<Node> link = node.entries.nodes; |
| 392 !link.isEmpty; | 392 !link.isEmpty; |
| 393 link = link.tail) { | 393 link = link.tail) { |
| 394 LiteralMapEntry entry = link.head; | 394 LiteralMapEntry entry = link.head; |
| 395 Constant key = evaluateConstant(entry.key); | 395 Constant key = evaluateConstant(entry.key); |
| 396 if (!key.isString() || entry.key.asStringNode() == null) { | |
| 397 compiler.reportFatalError( | |
| 398 entry.key, MessageKind.KEY_NOT_A_STRING_LITERAL); | |
| 399 } | |
| 400 StringConstant keyConstant = key; | |
| 401 if (!map.containsKey(key)) keys.add(key); | 396 if (!map.containsKey(key)) keys.add(key); |
| 402 map[key] = evaluateConstant(entry.value); | 397 map[key] = evaluateConstant(entry.value); |
| 403 } | 398 } |
| 399 bool hasNonStringKey = false; | |
|
ngeoffray
2013/09/13 07:30:15
Negating names are confusing. How about 'onlyStrin
Johnni Winther
2013/09/18 12:37:21
Done.
| |
| 404 List<Constant> values = <Constant>[]; | 400 List<Constant> values = <Constant>[]; |
| 405 Constant protoValue = null; | 401 Constant protoValue = null; |
| 406 for (StringConstant key in keys) { | 402 for (Constant key in keys) { |
| 407 if (key.value == MapConstant.PROTO_PROPERTY) { | 403 if (key.isString() && |
| 404 (key as dynamic).value == MapConstant.PROTO_PROPERTY) { | |
|
ngeoffray
2013/09/13 07:30:15
I'd prefer avoiding this as, and untype 'key'.
Johnni Winther
2013/09/18 12:37:21
Done.
| |
| 408 protoValue = map[key]; | 405 protoValue = map[key]; |
| 409 } else { | 406 } else { |
| 407 if (!key.isString()) { | |
|
ngeoffray
2013/09/13 07:30:15
Checking twice here. I suggest:
if (key.isString()
Johnni Winther
2013/09/18 12:37:21
Completely rewritten.
| |
| 408 hasNonStringKey = true; | |
| 409 } | |
| 410 values.add(map[key]); | 410 values.add(map[key]); |
| 411 } | 411 } |
| 412 } | 412 } |
| 413 bool hasProtoKey = (protoValue != null); | 413 bool hasProtoKey = (protoValue != null); |
| 414 InterfaceType sourceType = elements.getType(node); | 414 InterfaceType sourceType = elements.getType(node); |
| 415 Link<DartType> arguments = | 415 Link<DartType> arguments = |
| 416 new Link<DartType>.fromList([compiler.stringClass.rawType]); | 416 new Link<DartType>.fromList([compiler.stringClass.rawType]); |
| 417 DartType keysType = new InterfaceType(compiler.listClass, arguments); | 417 DartType keysType = new InterfaceType(compiler.listClass, arguments); |
| 418 ListConstant keysList = new ListConstant(keysType, keys); | 418 ListConstant keysList = new ListConstant(keysType, keys); |
| 419 handler.registerCompileTimeConstant(keysList, elements); | 419 if (!hasNonStringKey) { |
| 420 SourceString className = hasProtoKey | 420 handler.registerCompileTimeConstant(keysList, elements); |
| 421 ? MapConstant.DART_PROTO_CLASS | 421 } |
| 422 : MapConstant.DART_CLASS; | 422 SourceString className = hasNonStringKey |
| 423 ? MapConstant.DART_GENERAL_CLASS | |
| 424 : (hasProtoKey ? MapConstant.DART_PROTO_CLASS | |
| 425 : MapConstant.DART_STRING_CLASS); | |
| 423 ClassElement classElement = compiler.jsHelperLibrary.find(className); | 426 ClassElement classElement = compiler.jsHelperLibrary.find(className); |
| 424 classElement.ensureResolved(compiler); | 427 classElement.ensureResolved(compiler); |
| 425 Link<DartType> typeArgument = sourceType.typeArguments.tail; | 428 Link<DartType> typeArgument = sourceType.typeArguments; |
|
ngeoffray
2013/09/13 07:30:15
What is this change?
Johnni Winther
2013/09/18 12:37:21
ConstantMap<V> implements Map<String, V> (and frie
| |
| 426 InterfaceType type = new InterfaceType(classElement, typeArgument); | 429 InterfaceType type = new InterfaceType(classElement, typeArgument); |
| 427 handler.registerInstantiatedType(type, elements); | 430 handler.registerInstantiatedType(type, elements); |
| 428 Constant constant = new MapConstant(type, keysList, values, protoValue); | 431 Constant constant = |
| 432 new MapConstant(type, keysList, values, protoValue, hasNonStringKey); | |
| 429 handler.registerCompileTimeConstant(constant, elements); | 433 handler.registerCompileTimeConstant(constant, elements); |
| 430 return constant; | 434 return constant; |
| 431 } | 435 } |
| 432 | 436 |
| 433 Constant visitLiteralNull(LiteralNull node) { | 437 Constant visitLiteralNull(LiteralNull node) { |
| 434 return constantSystem.createNull(); | 438 return constantSystem.createNull(); |
| 435 } | 439 } |
| 436 | 440 |
| 437 Constant visitLiteralString(LiteralString node) { | 441 Constant visitLiteralString(LiteralString node) { |
| 438 handler.registerStringInstance(elements); | 442 handler.registerStringInstance(elements); |
| (...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 949 if (fieldValue == null) { | 953 if (fieldValue == null) { |
| 950 // Use the default value. | 954 // Use the default value. |
| 951 fieldValue = handler.compileConstant(field); | 955 fieldValue = handler.compileConstant(field); |
| 952 } | 956 } |
| 953 jsNewArguments.add(fieldValue); | 957 jsNewArguments.add(fieldValue); |
| 954 }, | 958 }, |
| 955 includeSuperAndInjectedMembers: true); | 959 includeSuperAndInjectedMembers: true); |
| 956 return jsNewArguments; | 960 return jsNewArguments; |
| 957 } | 961 } |
| 958 } | 962 } |
| OLD | NEW |