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; |
11 bool isFalse() => false; | 11 bool isFalse() => false; |
12 bool isInt() => false; | 12 bool isInt() => false; |
13 bool isDouble() => false; | 13 bool isDouble() => false; |
14 bool isNum() => false; | 14 bool isNum() => false; |
15 bool isString() => false; | 15 bool isString() => false; |
16 bool isList() => false; | 16 bool isList() => false; |
17 bool isMap() => false; | 17 bool isMap() => false; |
18 bool isConstructedObject() => false; | 18 bool isConstructedObject() => false; |
19 bool isFunction() => false; | |
19 /** Returns true if the constant is null, a bool, a number or a string. */ | 20 /** Returns true if the constant is null, a bool, a number or a string. */ |
20 bool isPrimitive() => false; | 21 bool isPrimitive() => false; |
21 /** Returns true if the constant is a list, a map or a constructed object. */ | 22 /** Returns true if the constant is a list, a map or a constructed object. */ |
22 bool isObject() => false; | 23 bool isObject() => false; |
23 | 24 |
24 bool isNaN() => false; | 25 bool isNaN() => false; |
25 | 26 |
26 abstract void _writeJsCode(CodeBuffer buffer, ConstantHandler handler); | 27 abstract void _writeJsCode(CodeBuffer buffer, ConstantHandler handler); |
27 /** | 28 /** |
28 * Unless the constant can be emitted multiple times (as for numbers and | 29 * Unless the constant can be emitted multiple times (as for numbers and |
29 * strings) adds its canonical name to the buffer. | 30 * strings) adds its canonical name to the buffer. |
30 */ | 31 */ |
31 abstract void _writeCanonicalizedJsCode(CodeBuffer buffer, | 32 abstract void _writeCanonicalizedJsCode(CodeBuffer buffer, |
32 ConstantHandler handler); | 33 ConstantHandler handler); |
33 abstract List<Constant> getDependencies(); | 34 abstract List<Constant> getDependencies(); |
34 } | 35 } |
35 | 36 |
37 class FunctionConstant extends Constant{ | |
kasperl
2012/08/27 13:17:26
Space between Constant and {.
ngeoffray
2012/08/27 14:09:09
Done.
| |
38 Element element; | |
39 | |
40 FunctionConstant(this.element); | |
41 | |
42 bool isFunction() => true; | |
43 | |
44 bool operator ==(var other) { | |
45 if (other is !FunctionConstant) return false; | |
46 return other.element === element; | |
47 } | |
48 | |
49 String toString() => element.toString(); | |
50 List<Constant> getDependencies() => const <Constant>[]; | |
51 DartString toDartString() { | |
52 return new DartString.literal(element.name.slowToString()); | |
53 } | |
54 | |
55 void _writeCanonicalizedJsCode(CodeBuffer buffer, ConstantHandler handler) { | |
kasperl
2012/08/27 13:17:26
Don't you need to also provide an implementation o
ngeoffray
2012/08/27 14:09:09
Yes, but it will never be called because there's n
| |
56 buffer.add(handler.compiler.namer.isolatePropertiesAccess(element)); | |
57 } | |
58 | |
59 int hashCode() => element.hashCode(); | |
60 } | |
61 | |
36 class PrimitiveConstant extends Constant { | 62 class PrimitiveConstant extends Constant { |
37 abstract get value; | 63 abstract get value; |
38 const PrimitiveConstant(); | 64 const PrimitiveConstant(); |
39 bool isPrimitive() => true; | 65 bool isPrimitive() => true; |
40 | 66 |
41 bool operator ==(var other) { | 67 bool operator ==(var other) { |
42 if (other is !PrimitiveConstant) return false; | 68 if (other is !PrimitiveConstant) return false; |
43 PrimitiveConstant otherPrimitive = other; | 69 PrimitiveConstant otherPrimitive = other; |
44 // We use == instead of === so that DartStrings compare correctly. | 70 // We use == instead of === so that DartStrings compare correctly. |
45 return value == otherPrimitive.value; | 71 return value == otherPrimitive.value; |
(...skipping 791 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
837 // TODO(floitsch): provide better error-messages. | 863 // TODO(floitsch): provide better error-messages. |
838 Constant visitSend(Send send) { | 864 Constant visitSend(Send send) { |
839 Element element = elements[send]; | 865 Element element = elements[send]; |
840 if (Elements.isStaticOrTopLevelField(element)) { | 866 if (Elements.isStaticOrTopLevelField(element)) { |
841 if (element.modifiers === null || | 867 if (element.modifiers === null || |
842 // TODO(johnniwinther): This should eventually be [isConst]. | 868 // TODO(johnniwinther): This should eventually be [isConst]. |
843 !element.modifiers.isFinalOrConst()) { | 869 !element.modifiers.isFinalOrConst()) { |
844 error(send); | 870 error(send); |
845 } | 871 } |
846 return compiler.compileVariable(element); | 872 return compiler.compileVariable(element); |
873 } else if (Elements.isStaticOrTopLevelFunction(element) | |
874 && send.isPropertyAccess) { | |
875 compiler.codegenWorld.staticFunctionsNeedingGetter.add(element); | |
kasperl
2012/08/27 13:17:26
Could this registration happen later in the codege
ngeoffray
2012/08/27 14:09:09
It has to be done here, the codegen only handles u
| |
876 return new FunctionConstant(element); | |
847 } else if (send.isPrefix) { | 877 } else if (send.isPrefix) { |
848 assert(send.isOperator); | 878 assert(send.isOperator); |
849 Constant receiverConstant = evaluate(send.receiver); | 879 Constant receiverConstant = evaluate(send.receiver); |
850 Operator op = send.selector; | 880 Operator op = send.selector; |
851 Constant folded; | 881 Constant folded; |
852 switch (op.source.stringValue) { | 882 switch (op.source.stringValue) { |
853 case "!": | 883 case "!": |
854 folded = const NotOperation().fold(receiverConstant); | 884 folded = const NotOperation().fold(receiverConstant); |
855 break; | 885 break; |
856 case "-": | 886 case "-": |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1170 Constant fieldValue = fieldValues[field]; | 1200 Constant fieldValue = fieldValues[field]; |
1171 if (fieldValue === null) { | 1201 if (fieldValue === null) { |
1172 // Use the default value. | 1202 // Use the default value. |
1173 fieldValue = compiler.compileVariable(field); | 1203 fieldValue = compiler.compileVariable(field); |
1174 } | 1204 } |
1175 jsNewArguments.add(fieldValue); | 1205 jsNewArguments.add(fieldValue); |
1176 }); | 1206 }); |
1177 return jsNewArguments; | 1207 return jsNewArguments; |
1178 } | 1208 } |
1179 } | 1209 } |
OLD | NEW |