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 class Constant implements Hashable { | 5 class Constant implements Hashable { |
6 const Constant(); | 6 const Constant(); |
7 | 7 |
8 bool isNull() => false; | 8 bool isNull() => false; |
9 bool isBool() => false; | 9 bool isBool() => false; |
10 bool isTrue() => false; | 10 bool isTrue() => false; |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 StringConstant otherString = other; | 282 StringConstant otherString = other; |
283 return (_hashCode == otherString._hashCode) && (value == otherString.value); | 283 return (_hashCode == otherString._hashCode) && (value == otherString.value); |
284 } | 284 } |
285 | 285 |
286 int hashCode() => _hashCode; | 286 int hashCode() => _hashCode; |
287 DartString toDartString() => value; | 287 DartString toDartString() => value; |
288 int get length => value.length; | 288 int get length => value.length; |
289 } | 289 } |
290 | 290 |
291 class ObjectConstant extends Constant { | 291 class ObjectConstant extends Constant { |
292 final Type type; | 292 final DartType type; |
293 | 293 |
294 ObjectConstant(this.type); | 294 ObjectConstant(this.type); |
295 bool isObject() => true; | 295 bool isObject() => true; |
296 | 296 |
297 // TODO(1603): The class should be marked as abstract, but the VM doesn't | 297 // TODO(1603): The class should be marked as abstract, but the VM doesn't |
298 // currently allow this. | 298 // currently allow this. |
299 abstract int hashCode(); | 299 abstract int hashCode(); |
300 | 300 |
301 void _writeCanonicalizedJsCode(CodeBuffer buffer, ConstantHandler handler) { | 301 void _writeCanonicalizedJsCode(CodeBuffer buffer, ConstantHandler handler) { |
302 String name = handler.getNameForConstant(this); | 302 String name = handler.getNameForConstant(this); |
303 buffer.add(handler.compiler.namer.isolatePropertiesAccessForConstant(name)); | 303 buffer.add(handler.compiler.namer.isolatePropertiesAccessForConstant(name)); |
304 } | 304 } |
305 } | 305 } |
306 | 306 |
307 class ListConstant extends ObjectConstant { | 307 class ListConstant extends ObjectConstant { |
308 final List<Constant> entries; | 308 final List<Constant> entries; |
309 int _hashCode; | 309 int _hashCode; |
310 | 310 |
311 ListConstant(Type type, this.entries) : super(type) { | 311 ListConstant(DartType type, this.entries) : super(type) { |
312 // TODO(floitsch): create a better hash. | 312 // TODO(floitsch): create a better hash. |
313 int hash = 0; | 313 int hash = 0; |
314 for (Constant input in entries) hash ^= input.hashCode(); | 314 for (Constant input in entries) hash ^= input.hashCode(); |
315 _hashCode = hash; | 315 _hashCode = hash; |
316 } | 316 } |
317 bool isList() => true; | 317 bool isList() => true; |
318 | 318 |
319 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { | 319 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { |
320 // TODO(floitsch): we should not need to go through the compiler to make | 320 // TODO(floitsch): we should not need to go through the compiler to make |
321 // the list constant. | 321 // the list constant. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 static const SourceString LENGTH_NAME = const SourceString("length"); | 362 static const SourceString LENGTH_NAME = const SourceString("length"); |
363 static const SourceString JS_OBJECT_NAME = const SourceString("_jsObject"); | 363 static const SourceString JS_OBJECT_NAME = const SourceString("_jsObject"); |
364 static const SourceString KEYS_NAME = const SourceString("_keys"); | 364 static const SourceString KEYS_NAME = const SourceString("_keys"); |
365 static const SourceString PROTO_VALUE = const SourceString("_protoValue"); | 365 static const SourceString PROTO_VALUE = const SourceString("_protoValue"); |
366 | 366 |
367 final ListConstant keys; | 367 final ListConstant keys; |
368 final List<Constant> values; | 368 final List<Constant> values; |
369 final Constant protoValue; | 369 final Constant protoValue; |
370 int _hashCode; | 370 int _hashCode; |
371 | 371 |
372 MapConstant(Type type, this.keys, this.values, this.protoValue) | 372 MapConstant(DartType type, this.keys, this.values, this.protoValue) |
373 : super(type) { | 373 : super(type) { |
374 // TODO(floitsch): create a better hash. | 374 // TODO(floitsch): create a better hash. |
375 int hash = 0; | 375 int hash = 0; |
376 for (Constant value in values) hash ^= value.hashCode(); | 376 for (Constant value in values) hash ^= value.hashCode(); |
377 _hashCode = hash; | 377 _hashCode = hash; |
378 } | 378 } |
379 bool isMap() => true; | 379 bool isMap() => true; |
380 | 380 |
381 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { | 381 void _writeJsCode(CodeBuffer buffer, ConstantHandler handler) { |
382 | 382 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 return result; | 458 return result; |
459 } | 459 } |
460 | 460 |
461 int get length => keys.length; | 461 int get length => keys.length; |
462 } | 462 } |
463 | 463 |
464 class ConstructedConstant extends ObjectConstant { | 464 class ConstructedConstant extends ObjectConstant { |
465 final List<Constant> fields; | 465 final List<Constant> fields; |
466 int _hashCode; | 466 int _hashCode; |
467 | 467 |
468 ConstructedConstant(Type type, this.fields) : super(type) { | 468 ConstructedConstant(DartType type, this.fields) : super(type) { |
469 assert(type !== null); | 469 assert(type !== null); |
470 // TODO(floitsch): create a better hash. | 470 // TODO(floitsch): create a better hash. |
471 int hash = 0; | 471 int hash = 0; |
472 for (Constant field in fields) { | 472 for (Constant field in fields) { |
473 hash ^= field.hashCode(); | 473 hash ^= field.hashCode(); |
474 } | 474 } |
475 hash ^= type.element.hashCode(); | 475 hash ^= type.element.hashCode(); |
476 _hashCode = hash; | 476 _hashCode = hash; |
477 } | 477 } |
478 bool isConstructedObject() => true; | 478 bool isConstructedObject() => true; |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
774 | 774 |
775 Constant visitLiteralList(LiteralList node) { | 775 Constant visitLiteralList(LiteralList node) { |
776 if (!node.isConst()) error(node); | 776 if (!node.isConst()) error(node); |
777 List<Constant> arguments = <Constant>[]; | 777 List<Constant> arguments = <Constant>[]; |
778 for (Link<Node> link = node.elements.nodes; | 778 for (Link<Node> link = node.elements.nodes; |
779 !link.isEmpty(); | 779 !link.isEmpty(); |
780 link = link.tail) { | 780 link = link.tail) { |
781 arguments.add(evaluate(link.head)); | 781 arguments.add(evaluate(link.head)); |
782 } | 782 } |
783 // TODO(floitsch): get type from somewhere. | 783 // TODO(floitsch): get type from somewhere. |
784 Type type = null; | 784 DartType type = null; |
785 Constant constant = new ListConstant(type, arguments); | 785 Constant constant = new ListConstant(type, arguments); |
786 compiler.constantHandler.registerCompileTimeConstant(constant); | 786 compiler.constantHandler.registerCompileTimeConstant(constant); |
787 return constant; | 787 return constant; |
788 } | 788 } |
789 | 789 |
790 Constant visitLiteralMap(LiteralMap node) { | 790 Constant visitLiteralMap(LiteralMap node) { |
791 if (!node.isConst()) error(node); | 791 if (!node.isConst()) error(node); |
792 List<StringConstant> keys = <StringConstant>[]; | 792 List<StringConstant> keys = <StringConstant>[]; |
793 Map<StringConstant, Constant> map = new Map<StringConstant, Constant>(); | 793 Map<StringConstant, Constant> map = new Map<StringConstant, Constant>(); |
794 for (Link<Node> link = node.entries.nodes; | 794 for (Link<Node> link = node.entries.nodes; |
(...skipping 13 matching lines...) Expand all Loading... |
808 Constant protoValue = null; | 808 Constant protoValue = null; |
809 for (StringConstant key in keys) { | 809 for (StringConstant key in keys) { |
810 if (key.value == const LiteralDartString(MapConstant.PROTO_PROPERTY)) { | 810 if (key.value == const LiteralDartString(MapConstant.PROTO_PROPERTY)) { |
811 protoValue = map[key]; | 811 protoValue = map[key]; |
812 } else { | 812 } else { |
813 values.add(map[key]); | 813 values.add(map[key]); |
814 } | 814 } |
815 } | 815 } |
816 bool hasProtoKey = (protoValue !== null); | 816 bool hasProtoKey = (protoValue !== null); |
817 // TODO(floitsch): this should be a List<String> type. | 817 // TODO(floitsch): this should be a List<String> type. |
818 Type keysType = null; | 818 DartType keysType = null; |
819 ListConstant keysList = new ListConstant(keysType, keys); | 819 ListConstant keysList = new ListConstant(keysType, keys); |
820 compiler.constantHandler.registerCompileTimeConstant(keysList); | 820 compiler.constantHandler.registerCompileTimeConstant(keysList); |
821 SourceString className = hasProtoKey | 821 SourceString className = hasProtoKey |
822 ? MapConstant.DART_PROTO_CLASS | 822 ? MapConstant.DART_PROTO_CLASS |
823 : MapConstant.DART_CLASS; | 823 : MapConstant.DART_CLASS; |
824 ClassElement classElement = compiler.jsHelperLibrary.find(className); | 824 ClassElement classElement = compiler.jsHelperLibrary.find(className); |
825 classElement.ensureResolved(compiler); | 825 classElement.ensureResolved(compiler); |
826 // TODO(floitsch): copy over the generic type. | 826 // TODO(floitsch): copy over the generic type. |
827 Type type = new InterfaceType(classElement); | 827 DartType type = new InterfaceType(classElement); |
828 compiler.registerInstantiatedClass(classElement); | 828 compiler.registerInstantiatedClass(classElement); |
829 Constant constant = new MapConstant(type, keysList, values, protoValue); | 829 Constant constant = new MapConstant(type, keysList, values, protoValue); |
830 compiler.constantHandler.registerCompileTimeConstant(constant); | 830 compiler.constantHandler.registerCompileTimeConstant(constant); |
831 return constant; | 831 return constant; |
832 } | 832 } |
833 | 833 |
834 Constant visitLiteralNull(LiteralNull node) { | 834 Constant visitLiteralNull(LiteralNull node) { |
835 return new NullConstant(); | 835 return new NullConstant(); |
836 } | 836 } |
837 | 837 |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1038 Selector selector = elements.getSelector(send); | 1038 Selector selector = elements.getSelector(send); |
1039 List<Constant> arguments = | 1039 List<Constant> arguments = |
1040 evaluateArgumentsToConstructor(selector, send.arguments, constructor); | 1040 evaluateArgumentsToConstructor(selector, send.arguments, constructor); |
1041 ConstructorEvaluator evaluator = | 1041 ConstructorEvaluator evaluator = |
1042 new ConstructorEvaluator(constructor, compiler); | 1042 new ConstructorEvaluator(constructor, compiler); |
1043 evaluator.evaluateConstructorFieldValues(arguments); | 1043 evaluator.evaluateConstructorFieldValues(arguments); |
1044 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); | 1044 List<Constant> jsNewArguments = evaluator.buildJsNewArguments(classElement); |
1045 | 1045 |
1046 compiler.registerInstantiatedClass(classElement); | 1046 compiler.registerInstantiatedClass(classElement); |
1047 // TODO(floitsch): take generic types into account. | 1047 // TODO(floitsch): take generic types into account. |
1048 Type type = classElement.computeType(compiler); | 1048 DartType type = classElement.computeType(compiler); |
1049 Constant constant = new ConstructedConstant(type, jsNewArguments); | 1049 Constant constant = new ConstructedConstant(type, jsNewArguments); |
1050 compiler.constantHandler.registerCompileTimeConstant(constant); | 1050 compiler.constantHandler.registerCompileTimeConstant(constant); |
1051 return constant; | 1051 return constant; |
1052 } | 1052 } |
1053 | 1053 |
1054 Constant visitParenthesizedExpression(ParenthesizedExpression node) { | 1054 Constant visitParenthesizedExpression(ParenthesizedExpression node) { |
1055 return node.expression.accept(this); | 1055 return node.expression.accept(this); |
1056 } | 1056 } |
1057 | 1057 |
1058 error(Node node) { | 1058 error(Node node) { |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1210 Constant fieldValue = fieldValues[field]; | 1210 Constant fieldValue = fieldValues[field]; |
1211 if (fieldValue === null) { | 1211 if (fieldValue === null) { |
1212 // Use the default value. | 1212 // Use the default value. |
1213 fieldValue = compiler.compileVariable(field); | 1213 fieldValue = compiler.compileVariable(field); |
1214 } | 1214 } |
1215 jsNewArguments.add(fieldValue); | 1215 jsNewArguments.add(fieldValue); |
1216 }); | 1216 }); |
1217 return jsNewArguments; | 1217 return jsNewArguments; |
1218 } | 1218 } |
1219 } | 1219 } |
OLD | NEW |