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 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 882 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 893 return folded; | 893 return folded; |
| 894 } | 894 } |
| 895 return super.visitSend(send); | 895 return super.visitSend(send); |
| 896 } | 896 } |
| 897 | 897 |
| 898 visitSendSet(SendSet node) { | 898 visitSendSet(SendSet node) { |
| 899 error(node); | 899 error(node); |
| 900 } | 900 } |
| 901 | 901 |
| 902 Constant visitNewExpression(NewExpression node) { | 902 Constant visitNewExpression(NewExpression node) { |
| 903 List<Constant> compileArguments() { | |
| 904 if (!node.isConst()) error(node); | |
| 905 Send send = node.send; | |
| 906 List<Constant> arguments; | |
| 907 if (send.arguments.isEmpty()) { | |
| 908 arguments = const <Constant>[]; | |
| 909 } else { | |
| 910 arguments = <Constant>[]; | |
| 911 for (Link<Node> link = send.arguments; | |
| 912 !link.isEmpty(); | |
| 913 link = link.tail) { | |
| 914 arguments.add(evaluate(link.head)); | |
| 915 } | |
| 916 } | |
| 917 return arguments; | |
| 918 } | |
| 919 | |
| 920 void assignArgumentsToParameters( | 903 void assignArgumentsToParameters( |
| 921 List<Constant> arguments, | |
| 922 FunctionParameters parameters, | 904 FunctionParameters parameters, |
| 923 Map<Element, Constant> constructorDefinitions) { | 905 Map<Element, Constant> constructorDefinitions) { |
| 924 if (arguments.length != parameters.parameterCount) { | 906 Send send = node.send; |
| 925 if (arguments.length < parameters.parameterCount && | 907 if (send.arguments.isEmpty() && parameters.parameterCount == 0) return; |
| 926 arguments.length >= parameters.requiredParameterCount) { | 908 List<Constant> arguments = <Constant>[]; |
| 927 compiler.unimplemented("ConstantHandler with optional arguments", | 909 Selector selector = elements.getSelector(send); |
| 928 node: node); | 910 |
| 929 } else { | 911 Constant evaluateConstant(Element element) |
| 930 error(node); | 912 => constantHandler.compileVariable(element); |
| 931 } | 913 |
| 932 } | 914 Function visitArgument = evaluate; |
| 915 Function visitConstant = constantHandler.compileVariable; | |
|
ngeoffray
2012/03/15 10:24:58
Because this class is a visitor, I'd prefer avoidi
floitsch
2012/03/15 14:30:47
removed evaluateConstant (old dead code).
renamed
| |
| 916 bool succeeded = selector.addSendArgumentsToList( | |
| 917 send, arguments, parameters, visitArgument, visitConstant); | |
| 918 if (!succeeded) error(node); | |
| 919 | |
| 933 int index = 0; | 920 int index = 0; |
| 934 parameters.forEachParameter((Element parameter) { | 921 parameters.forEachParameter((Element parameter) { |
| 935 constructorDefinitions[parameter] = arguments[index++]; | 922 constructorDefinitions[parameter] = arguments[index++]; |
| 936 }); | 923 }); |
| 937 } | 924 } |
| 938 | 925 |
| 939 void compileInitializers(Link<Node> initializers, | 926 void compileInitializers(Link<Node> initializers, |
| 940 CompileTimeConstantEvaluator evaluator, | 927 CompileTimeConstantEvaluator evaluator, |
| 941 TreeElements constructorElements, | 928 TreeElements constructorElements, |
| 942 Map<Element, Constant> constructorDefinitions) { | 929 Map<Element, Constant> constructorDefinitions) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 973 } | 960 } |
| 974 fieldValues.add(fieldValue); | 961 fieldValues.add(fieldValue); |
| 975 } | 962 } |
| 976 } | 963 } |
| 977 if (classElement.superclass != compiler.coreLibrary.find(Types.OBJECT)) { | 964 if (classElement.superclass != compiler.coreLibrary.find(Types.OBJECT)) { |
| 978 compiler.unimplemented("ConstantHandler with super", node: node); | 965 compiler.unimplemented("ConstantHandler with super", node: node); |
| 979 } | 966 } |
| 980 return fieldValues; | 967 return fieldValues; |
| 981 } | 968 } |
| 982 | 969 |
| 970 if (!node.isConst()) error(node); | |
| 971 | |
| 983 // TODO(floitsch): get the type from somewhere. | 972 // TODO(floitsch): get the type from somewhere. |
| 984 FunctionElement constructor = elements[node.send]; | 973 FunctionElement constructor = elements[node.send]; |
| 985 ClassElement classElement = constructor.enclosingElement; | 974 ClassElement classElement = constructor.enclosingElement; |
| 986 TreeElements constructorElements = | 975 TreeElements constructorElements = |
| 987 compiler.resolver.resolveMethodElement(constructor); | 976 compiler.resolver.resolveMethodElement(constructor); |
| 988 FunctionExpression functionNode = constructor.parseNode(compiler); | 977 FunctionExpression functionNode = constructor.parseNode(compiler); |
| 989 NodeList initializerList = functionNode.initializers; | 978 NodeList initializerList = functionNode.initializers; |
| 990 FunctionParameters parameters = constructor.computeParameters(compiler); | 979 FunctionParameters parameters = constructor.computeParameters(compiler); |
| 991 | 980 |
| 992 Map<Element, Constant> constructorDefinitions = | 981 Map<Element, Constant> constructorDefinitions = |
| 993 new Map<Element, Constant>(); | 982 new Map<Element, Constant>(); |
| 994 | 983 |
| 995 List<Constant> arguments = compileArguments(); | 984 assignArgumentsToParameters(parameters, constructorDefinitions); |
| 996 assignArgumentsToParameters(arguments, parameters, constructorDefinitions); | |
| 997 CompileTimeConstantEvaluator initializerEvaluator = | 985 CompileTimeConstantEvaluator initializerEvaluator = |
| 998 new CompileTimeConstantEvaluator.insideConstructor( | 986 new CompileTimeConstantEvaluator.insideConstructor( |
| 999 constantHandler, constructorElements, compiler, | 987 constantHandler, constructorElements, compiler, |
| 1000 constructorDefinitions); | 988 constructorDefinitions); |
| 1001 if (initializerList !== null) { | 989 if (initializerList !== null) { |
| 1002 Link<Node> initializers = functionNode.initializers.nodes; | 990 Link<Node> initializers = functionNode.initializers.nodes; |
| 1003 compileInitializers(initializers, | 991 compileInitializers(initializers, |
| 1004 initializerEvaluator, | 992 initializerEvaluator, |
| 1005 constructorElements, | 993 constructorElements, |
| 1006 constructorDefinitions); | 994 constructorDefinitions); |
| 1007 } | 995 } |
| 1008 List<Constant> fieldValues = | 996 List<Constant> fieldValues = |
| 1009 buildJsConstructorArguments(classElement, constructorDefinitions); | 997 buildJsConstructorArguments(classElement, constructorDefinitions); |
| 1010 | 998 |
| 1011 compiler.registerInstantiatedClass(classElement); | 999 compiler.registerInstantiatedClass(classElement); |
| 1012 Type type = new SimpleType(classElement.name, classElement); | 1000 Type type = new SimpleType(classElement.name, classElement); |
| 1013 Constant constant = new ConstructedConstant(type, fieldValues); | 1001 Constant constant = new ConstructedConstant(type, fieldValues); |
| 1014 constantHandler.registerCompileTimeConstant(constant); | 1002 constantHandler.registerCompileTimeConstant(constant); |
| 1015 return constant; | 1003 return constant; |
| 1016 } | 1004 } |
| 1017 | 1005 |
| 1018 error(Node node) { | 1006 error(Node node) { |
| 1019 // TODO(floitsch): get the list of constants that are currently compiled | 1007 // TODO(floitsch): get the list of constants that are currently compiled |
| 1020 // and present some kind of stack-trace. | 1008 // and present some kind of stack-trace. |
| 1021 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; | 1009 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; |
| 1022 compiler.reportError(node, new CompileTimeConstantError(kind, const [])); | 1010 compiler.reportError(node, new CompileTimeConstantError(kind, const [])); |
| 1023 } | 1011 } |
| 1024 } | 1012 } |
| OLD | NEW |