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

Side by Side Diff: frog/leg/compile_time_constants.dart

Issue 9705025: const constructors with optional arguments. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 8 years, 9 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 | frog/leg/resolver.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;
(...skipping 882 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 Function compileArgument = evaluate;
930 error(node); 912 Function compileConstant = constantHandler.compileVariable;
931 } 913 bool succeeded = selector.addSendArgumentsToList(
932 } 914 send, arguments, parameters, compileArgument, compileConstant);
915 if (!succeeded) error(node);
916
933 int index = 0; 917 int index = 0;
934 parameters.forEachParameter((Element parameter) { 918 parameters.forEachParameter((Element parameter) {
935 constructorDefinitions[parameter] = arguments[index++]; 919 constructorDefinitions[parameter] = arguments[index++];
936 }); 920 });
937 } 921 }
938 922
939 void compileInitializers(Link<Node> initializers, 923 void compileInitializers(Link<Node> initializers,
940 CompileTimeConstantEvaluator evaluator, 924 CompileTimeConstantEvaluator evaluator,
941 TreeElements constructorElements, 925 TreeElements constructorElements,
942 Map<Element, Constant> constructorDefinitions) { 926 Map<Element, Constant> constructorDefinitions) {
(...skipping 30 matching lines...) Expand all
973 } 957 }
974 fieldValues.add(fieldValue); 958 fieldValues.add(fieldValue);
975 } 959 }
976 } 960 }
977 if (classElement.superclass != compiler.coreLibrary.find(Types.OBJECT)) { 961 if (classElement.superclass != compiler.coreLibrary.find(Types.OBJECT)) {
978 compiler.unimplemented("ConstantHandler with super", node: node); 962 compiler.unimplemented("ConstantHandler with super", node: node);
979 } 963 }
980 return fieldValues; 964 return fieldValues;
981 } 965 }
982 966
967 if (!node.isConst()) error(node);
968
983 // TODO(floitsch): get the type from somewhere. 969 // TODO(floitsch): get the type from somewhere.
984 FunctionElement constructor = elements[node.send]; 970 FunctionElement constructor = elements[node.send];
985 ClassElement classElement = constructor.enclosingElement; 971 ClassElement classElement = constructor.enclosingElement;
986 TreeElements constructorElements = 972 TreeElements constructorElements =
987 compiler.resolver.resolveMethodElement(constructor); 973 compiler.resolver.resolveMethodElement(constructor);
988 FunctionExpression functionNode = constructor.parseNode(compiler); 974 FunctionExpression functionNode = constructor.parseNode(compiler);
989 NodeList initializerList = functionNode.initializers; 975 NodeList initializerList = functionNode.initializers;
990 FunctionParameters parameters = constructor.computeParameters(compiler); 976 FunctionParameters parameters = constructor.computeParameters(compiler);
991 977
992 Map<Element, Constant> constructorDefinitions = 978 Map<Element, Constant> constructorDefinitions =
993 new Map<Element, Constant>(); 979 new Map<Element, Constant>();
994 980
995 List<Constant> arguments = compileArguments(); 981 assignArgumentsToParameters(parameters, constructorDefinitions);
996 assignArgumentsToParameters(arguments, parameters, constructorDefinitions);
997 CompileTimeConstantEvaluator initializerEvaluator = 982 CompileTimeConstantEvaluator initializerEvaluator =
998 new CompileTimeConstantEvaluator.insideConstructor( 983 new CompileTimeConstantEvaluator.insideConstructor(
999 constantHandler, constructorElements, compiler, 984 constantHandler, constructorElements, compiler,
1000 constructorDefinitions); 985 constructorDefinitions);
1001 if (initializerList !== null) { 986 if (initializerList !== null) {
1002 Link<Node> initializers = functionNode.initializers.nodes; 987 Link<Node> initializers = functionNode.initializers.nodes;
1003 compileInitializers(initializers, 988 compileInitializers(initializers,
1004 initializerEvaluator, 989 initializerEvaluator,
1005 constructorElements, 990 constructorElements,
1006 constructorDefinitions); 991 constructorDefinitions);
1007 } 992 }
1008 List<Constant> fieldValues = 993 List<Constant> fieldValues =
1009 buildJsConstructorArguments(classElement, constructorDefinitions); 994 buildJsConstructorArguments(classElement, constructorDefinitions);
1010 995
1011 compiler.registerInstantiatedClass(classElement); 996 compiler.registerInstantiatedClass(classElement);
1012 Type type = new SimpleType(classElement.name, classElement); 997 Type type = new SimpleType(classElement.name, classElement);
1013 Constant constant = new ConstructedConstant(type, fieldValues); 998 Constant constant = new ConstructedConstant(type, fieldValues);
1014 constantHandler.registerCompileTimeConstant(constant); 999 constantHandler.registerCompileTimeConstant(constant);
1015 return constant; 1000 return constant;
1016 } 1001 }
1017 1002
1018 error(Node node) { 1003 error(Node node) {
1019 // TODO(floitsch): get the list of constants that are currently compiled 1004 // TODO(floitsch): get the list of constants that are currently compiled
1020 // and present some kind of stack-trace. 1005 // and present some kind of stack-trace.
1021 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT; 1006 MessageKind kind = MessageKind.NOT_A_COMPILE_TIME_CONSTANT;
1022 compiler.reportError(node, new CompileTimeConstantError(kind, const [])); 1007 compiler.reportError(node, new CompileTimeConstantError(kind, const []));
1023 } 1008 }
1024 } 1009 }
OLDNEW
« no previous file with comments | « no previous file | frog/leg/resolver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698