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