Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: lib/compiler/implementation/compile_time_constants.dart

Issue 10872059: Static and top-level methods can be compile time constants. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | lib/compiler/implementation/ssa/codegen.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 {
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 _writeJsCode(CodeBuffer buffer, ConstantHandler handler) {
56 compiler.internalError(
57 "A constant function does not need specific JS code");
58 }
59
60 void _writeCanonicalizedJsCode(CodeBuffer buffer, ConstantHandler handler) {
61 buffer.add(handler.compiler.namer.isolatePropertiesAccess(element));
62 }
63
64 int hashCode() => element.hashCode();
ahe 2012/08/28 13:35:54 You should make sure that FunctionConstant has a d
65 }
66
36 class PrimitiveConstant extends Constant { 67 class PrimitiveConstant extends Constant {
37 abstract get value; 68 abstract get value;
38 const PrimitiveConstant(); 69 const PrimitiveConstant();
39 bool isPrimitive() => true; 70 bool isPrimitive() => true;
40 71
41 bool operator ==(var other) { 72 bool operator ==(var other) {
42 if (other is !PrimitiveConstant) return false; 73 if (other is !PrimitiveConstant) return false;
43 PrimitiveConstant otherPrimitive = other; 74 PrimitiveConstant otherPrimitive = other;
44 // We use == instead of === so that DartStrings compare correctly. 75 // We use == instead of === so that DartStrings compare correctly.
45 return value == otherPrimitive.value; 76 return value == otherPrimitive.value;
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 final Set<VariableElement> pendingVariables; 523 final Set<VariableElement> pendingVariables;
493 524
494 ConstantHandler(Compiler compiler) 525 ConstantHandler(Compiler compiler)
495 : initialVariableValues = new Map<VariableElement, Dynamic>(), 526 : initialVariableValues = new Map<VariableElement, Dynamic>(),
496 compiledConstants = new Map<Constant, String>(), 527 compiledConstants = new Map<Constant, String>(),
497 pendingVariables = new Set<VariableElement>(), 528 pendingVariables = new Set<VariableElement>(),
498 super(compiler); 529 super(compiler);
499 String get name => 'ConstantHandler'; 530 String get name => 'ConstantHandler';
500 531
501 void registerCompileTimeConstant(Constant constant) { 532 void registerCompileTimeConstant(Constant constant) {
502 Function ifAbsentThunk = (() => compiler.namer.getFreshGlobalName("CTC")); 533 Function ifAbsentThunk = (() {
534 return constant.isFunction()
535 ? null : compiler.namer.getFreshGlobalName("CTC");
536 });
503 compiledConstants.putIfAbsent(constant, ifAbsentThunk); 537 compiledConstants.putIfAbsent(constant, ifAbsentThunk);
504 } 538 }
505 539
506 /** 540 /**
507 * Compiles the initial value of the given field and stores it in an internal 541 * Compiles the initial value of the given field and stores it in an internal
508 * map. 542 * map.
509 * 543 *
510 * [WorkItem] must contain a [VariableElement] refering to a global or 544 * [WorkItem] must contain a [VariableElement] refering to a global or
511 * static field. 545 * static field.
512 */ 546 */
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 // TODO(floitsch): provide better error-messages. 871 // TODO(floitsch): provide better error-messages.
838 Constant visitSend(Send send) { 872 Constant visitSend(Send send) {
839 Element element = elements[send]; 873 Element element = elements[send];
840 if (Elements.isStaticOrTopLevelField(element)) { 874 if (Elements.isStaticOrTopLevelField(element)) {
841 if (element.modifiers === null || 875 if (element.modifiers === null ||
842 // TODO(johnniwinther): This should eventually be [isConst]. 876 // TODO(johnniwinther): This should eventually be [isConst].
843 !element.modifiers.isFinalOrConst()) { 877 !element.modifiers.isFinalOrConst()) {
844 error(send); 878 error(send);
845 } 879 }
846 return compiler.compileVariable(element); 880 return compiler.compileVariable(element);
881 } else if (Elements.isStaticOrTopLevelFunction(element)
882 && send.isPropertyAccess) {
883 compiler.codegenWorld.staticFunctionsNeedingGetter.add(element);
884 Constant constant = new FunctionConstant(element);
885 compiler.constantHandler.registerCompileTimeConstant(constant);
886 return constant;
847 } else if (send.isPrefix) { 887 } else if (send.isPrefix) {
848 assert(send.isOperator); 888 assert(send.isOperator);
849 Constant receiverConstant = evaluate(send.receiver); 889 Constant receiverConstant = evaluate(send.receiver);
850 Operator op = send.selector; 890 Operator op = send.selector;
851 Constant folded; 891 Constant folded;
852 switch (op.source.stringValue) { 892 switch (op.source.stringValue) {
853 case "!": 893 case "!":
854 folded = const NotOperation().fold(receiverConstant); 894 folded = const NotOperation().fold(receiverConstant);
855 break; 895 break;
856 case "-": 896 case "-":
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
1170 Constant fieldValue = fieldValues[field]; 1210 Constant fieldValue = fieldValues[field];
1171 if (fieldValue === null) { 1211 if (fieldValue === null) {
1172 // Use the default value. 1212 // Use the default value.
1173 fieldValue = compiler.compileVariable(field); 1213 fieldValue = compiler.compileVariable(field);
1174 } 1214 }
1175 jsNewArguments.add(fieldValue); 1215 jsNewArguments.add(fieldValue);
1176 }); 1216 });
1177 return jsNewArguments; 1217 return jsNewArguments;
1178 } 1218 }
1179 } 1219 }
OLDNEW
« no previous file with comments | « no previous file | lib/compiler/implementation/ssa/codegen.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698