Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 // This code was auto-generated, is not intended to be edited, and is subject to | 5 // This code was auto-generated, is not intended to be edited, and is subject to |
| 6 // significant change. Please see the README file for more information. | 6 // significant change. Please see the README file for more information. |
| 7 | 7 |
| 8 library engine.constant; | 8 library engine.constant; |
| 9 | 9 |
| 10 import 'dart:collection'; | 10 import 'dart:collection'; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 148 * constants to be evaluated. | 148 * constants to be evaluated. |
| 149 */ | 149 */ |
| 150 class ConstantAstCloner extends AstCloner { | 150 class ConstantAstCloner extends AstCloner { |
| 151 ConstantAstCloner() : super(true); | 151 ConstantAstCloner() : super(true); |
| 152 | 152 |
| 153 @override | 153 @override |
| 154 InstanceCreationExpression visitInstanceCreationExpression( | 154 InstanceCreationExpression visitInstanceCreationExpression( |
| 155 InstanceCreationExpression node) { | 155 InstanceCreationExpression node) { |
| 156 InstanceCreationExpression expression = | 156 InstanceCreationExpression expression = |
| 157 super.visitInstanceCreationExpression(node); | 157 super.visitInstanceCreationExpression(node); |
| 158 expression.constantHandle = node.constantHandle; | 158 expression.staticElement = node.staticElement; |
| 159 return expression; | 159 return expression; |
| 160 } | 160 } |
| 161 | 161 |
| 162 @override | 162 @override |
| 163 RedirectingConstructorInvocation visitRedirectingConstructorInvocation( | 163 RedirectingConstructorInvocation visitRedirectingConstructorInvocation( |
| 164 RedirectingConstructorInvocation node) { | 164 RedirectingConstructorInvocation node) { |
| 165 RedirectingConstructorInvocation invocation = | 165 RedirectingConstructorInvocation invocation = |
| 166 super.visitRedirectingConstructorInvocation(node); | 166 super.visitRedirectingConstructorInvocation(node); |
| 167 invocation.staticElement = node.staticElement; | 167 invocation.staticElement = node.staticElement; |
| 168 return invocation; | 168 return invocation; |
| (...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 bool runtimeTypeMatch(DartObjectImpl obj, DartType type) { | 671 bool runtimeTypeMatch(DartObjectImpl obj, DartType type) { |
| 672 if (obj.isNull) { | 672 if (obj.isNull) { |
| 673 return true; | 673 return true; |
| 674 } | 674 } |
| 675 if (type.isUndefined) { | 675 if (type.isUndefined) { |
| 676 return false; | 676 return false; |
| 677 } | 677 } |
| 678 return obj.type.isSubtypeOf(type); | 678 return obj.type.isSubtypeOf(type); |
| 679 } | 679 } |
| 680 | 680 |
| 681 ConstructorElementImpl _getConstructorBase(ConstructorElement constructor) { | |
| 682 while (constructor is ConstructorMember) { | |
| 683 constructor = (constructor as ConstructorMember).baseElement; | |
| 684 } | |
| 685 return constructor; | |
| 686 } | |
| 687 | |
| 688 /** | 681 /** |
| 689 * Determine whether the given string is a valid name for a public symbol | 682 * Determine whether the given string is a valid name for a public symbol |
| 690 * (i.e. whether it is allowed for a call to the Symbol constructor). | 683 * (i.e. whether it is allowed for a call to the Symbol constructor). |
| 691 */ | 684 */ |
| 692 static bool isValidPublicSymbol(String name) => name.isEmpty || | 685 static bool isValidPublicSymbol(String name) => name.isEmpty || |
| 693 name == "void" || | 686 name == "void" || |
| 694 new JavaPatternMatcher(_PUBLIC_SYMBOL_PATTERN, name).matches(); | 687 new JavaPatternMatcher(_PUBLIC_SYMBOL_PATTERN, name).matches(); |
| 688 | |
| 689 static ConstructorElementImpl _getConstructorBase( | |
| 690 ConstructorElement constructor) { | |
| 691 while (constructor is ConstructorMember) { | |
| 692 constructor = (constructor as ConstructorMember).baseElement; | |
| 693 } | |
| 694 return constructor; | |
| 695 } | |
| 695 } | 696 } |
| 696 | 697 |
| 697 /** | 698 /** |
| 698 * Interface used by unit tests to verify correct dependency analysis during | 699 * Interface used by unit tests to verify correct dependency analysis during |
| 699 * constant evaluation. | 700 * constant evaluation. |
| 700 */ | 701 */ |
| 701 abstract class ConstantEvaluationValidator { | 702 abstract class ConstantEvaluationValidator { |
| 702 /** | 703 /** |
| 703 * This method is called just before computing the constant value associated | 704 * This method is called just before computing the constant value associated |
| 704 * with [constNode]. Unit tests will override this method to introduce | 705 * with [constNode]. Unit tests will override this method to introduce |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 858 new HashMap<PotentiallyConstVariableElement, VariableDeclaration>(); | 859 new HashMap<PotentiallyConstVariableElement, VariableDeclaration>(); |
| 859 | 860 |
| 860 /** | 861 /** |
| 861 * A table mapping constant constructors to the declarations of those | 862 * A table mapping constant constructors to the declarations of those |
| 862 * constructors. | 863 * constructors. |
| 863 */ | 864 */ |
| 864 final HashMap<ConstructorElement, ConstructorDeclaration> constructorMap = | 865 final HashMap<ConstructorElement, ConstructorDeclaration> constructorMap = |
| 865 new HashMap<ConstructorElement, ConstructorDeclaration>(); | 866 new HashMap<ConstructorElement, ConstructorDeclaration>(); |
| 866 | 867 |
| 867 /** | 868 /** |
| 868 * A collection of constant constructor invocations. | |
| 869 */ | |
| 870 final List<InstanceCreationExpression> constructorInvocations = | |
| 871 new List<InstanceCreationExpression>(); | |
| 872 | |
| 873 /** | |
| 874 * A collection of annotations. | 869 * A collection of annotations. |
| 875 */ | 870 */ |
| 876 final List<Annotation> annotations = <Annotation>[]; | 871 final List<Annotation> annotations = <Annotation>[]; |
| 877 | 872 |
| 878 /** | 873 /** |
| 879 * True if instance variables marked as "final" should be treated as "const". | 874 * True if instance variables marked as "final" should be treated as "const". |
| 880 */ | 875 */ |
| 881 bool treatFinalInstanceVarAsConst = false; | 876 bool treatFinalInstanceVarAsConst = false; |
| 882 | 877 |
| 883 @override | 878 @override |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 909 if (node.constKeyword != null) { | 904 if (node.constKeyword != null) { |
| 910 ConstructorElement element = node.element; | 905 ConstructorElement element = node.element; |
| 911 if (element != null) { | 906 if (element != null) { |
| 912 constructorMap[element] = node; | 907 constructorMap[element] = node; |
| 913 } | 908 } |
| 914 } | 909 } |
| 915 return null; | 910 return null; |
| 916 } | 911 } |
| 917 | 912 |
| 918 @override | 913 @override |
| 919 Object visitInstanceCreationExpression(InstanceCreationExpression node) { | |
| 920 super.visitInstanceCreationExpression(node); | |
| 921 if (node.isConst) { | |
| 922 constructorInvocations.add(node); | |
| 923 } | |
| 924 return null; | |
| 925 } | |
| 926 | |
| 927 @override | |
| 928 Object visitVariableDeclaration(VariableDeclaration node) { | 914 Object visitVariableDeclaration(VariableDeclaration node) { |
| 929 super.visitVariableDeclaration(node); | 915 super.visitVariableDeclaration(node); |
| 930 Expression initializer = node.initializer; | 916 Expression initializer = node.initializer; |
| 931 VariableElement element = node.element; | 917 VariableElement element = node.element; |
| 932 if (initializer != null && | 918 if (initializer != null && |
| 933 (node.isConst || | 919 (node.isConst || |
| 934 treatFinalInstanceVarAsConst && | 920 treatFinalInstanceVarAsConst && |
| 935 element is FieldElement && | 921 element is FieldElement && |
| 936 node.isFinal && | 922 node.isFinal && |
| 937 !element.isStatic)) { | 923 !element.isStatic)) { |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 983 */ | 969 */ |
| 984 HashMap<PotentiallyConstVariableElement, VariableDeclaration> _variableDeclara tionMap; | 970 HashMap<PotentiallyConstVariableElement, VariableDeclaration> _variableDeclara tionMap; |
| 985 | 971 |
| 986 /** | 972 /** |
| 987 * A table mapping constant constructors to the declarations of those | 973 * A table mapping constant constructors to the declarations of those |
| 988 * constructors. | 974 * constructors. |
| 989 */ | 975 */ |
| 990 HashMap<ConstructorElement, ConstructorDeclaration> constructorDeclarationMap; | 976 HashMap<ConstructorElement, ConstructorDeclaration> constructorDeclarationMap; |
| 991 | 977 |
| 992 /** | 978 /** |
| 993 * A collection of constant constructor invocations. | |
| 994 */ | |
| 995 List<InstanceCreationExpression> _constructorInvocations; | |
| 996 | |
| 997 /** | |
| 998 * A collection of annotations. | 979 * A collection of annotations. |
| 999 */ | 980 */ |
| 1000 List<Annotation> _annotations; | 981 List<Annotation> _annotations; |
| 1001 | 982 |
| 1002 /** | 983 /** |
| 1003 * The evaluation engine that does the work of evaluating instance creation | 984 * The evaluation engine that does the work of evaluating instance creation |
| 1004 * expressions. | 985 * expressions. |
| 1005 */ | 986 */ |
| 1006 final ConstantEvaluationEngine evaluationEngine; | 987 final ConstantEvaluationEngine evaluationEngine; |
| 1007 | 988 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 1024 unit.accept(_constantFinder); | 1005 unit.accept(_constantFinder); |
| 1025 } | 1006 } |
| 1026 | 1007 |
| 1027 /** | 1008 /** |
| 1028 * Compute values for all of the constants in the compilation units that were | 1009 * Compute values for all of the constants in the compilation units that were |
| 1029 * added. | 1010 * added. |
| 1030 */ | 1011 */ |
| 1031 void computeValues() { | 1012 void computeValues() { |
| 1032 _variableDeclarationMap = _constantFinder.variableMap; | 1013 _variableDeclarationMap = _constantFinder.variableMap; |
| 1033 constructorDeclarationMap = _constantFinder.constructorMap; | 1014 constructorDeclarationMap = _constantFinder.constructorMap; |
| 1034 _constructorInvocations = _constantFinder.constructorInvocations; | |
| 1035 _annotations = _constantFinder.annotations; | 1015 _annotations = _constantFinder.annotations; |
| 1036 _variableDeclarationMap.values.forEach((VariableDeclaration declaration) { | 1016 _variableDeclarationMap.values.forEach((VariableDeclaration declaration) { |
| 1037 ReferenceFinder referenceFinder = new ReferenceFinder(declaration, | 1017 ReferenceFinder referenceFinder = new ReferenceFinder(declaration, |
| 1038 referenceGraph, _variableDeclarationMap, constructorDeclarationMap); | 1018 referenceGraph, _variableDeclarationMap, constructorDeclarationMap); |
| 1039 referenceGraph.addNode(declaration); | 1019 referenceGraph.addNode(declaration); |
| 1040 declaration.initializer.accept(referenceFinder); | 1020 declaration.initializer.accept(referenceFinder); |
| 1041 }); | 1021 }); |
| 1042 constructorDeclarationMap.forEach((ConstructorElementImpl element, | 1022 constructorDeclarationMap.forEach((ConstructorElementImpl element, |
| 1043 ConstructorDeclaration declaration) { | 1023 ConstructorDeclaration declaration) { |
| 1044 element.isCycleFree = false; | 1024 element.isCycleFree = false; |
| 1045 ConstructorElement redirectedConstructor = | 1025 ConstructorElement redirectedConstructor = |
| 1046 evaluationEngine.getConstRedirectedConstructor(element); | 1026 evaluationEngine.getConstRedirectedConstructor(element); |
| 1047 if (redirectedConstructor != null) { | 1027 if (redirectedConstructor != null) { |
| 1048 ConstructorElement redirectedConstructorBase = | 1028 ConstructorElement redirectedConstructorBase = |
| 1049 evaluationEngine._getConstructorBase(redirectedConstructor); | 1029 ConstantEvaluationEngine._getConstructorBase(redirectedConstructor); |
| 1050 ConstructorDeclaration redirectedConstructorDeclaration = | 1030 ConstructorDeclaration redirectedConstructorDeclaration = |
| 1051 findConstructorDeclaration(redirectedConstructorBase); | 1031 findConstructorDeclaration(redirectedConstructorBase); |
| 1052 referenceGraph.addEdge(declaration, redirectedConstructorDeclaration); | 1032 referenceGraph.addEdge(declaration, redirectedConstructorDeclaration); |
| 1053 return; | 1033 return; |
| 1054 } | 1034 } |
| 1055 ReferenceFinder referenceFinder = new ReferenceFinder(declaration, | 1035 ReferenceFinder referenceFinder = new ReferenceFinder(declaration, |
| 1056 referenceGraph, _variableDeclarationMap, constructorDeclarationMap); | 1036 referenceGraph, _variableDeclarationMap, constructorDeclarationMap); |
| 1057 referenceGraph.addNode(declaration); | 1037 referenceGraph.addNode(declaration); |
| 1058 bool superInvocationFound = false; | 1038 bool superInvocationFound = false; |
| 1059 NodeList<ConstructorInitializer> initializers = declaration.initializers; | 1039 NodeList<ConstructorInitializer> initializers = declaration.initializers; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1095 Expression defaultValue = parameter.defaultValue; | 1075 Expression defaultValue = parameter.defaultValue; |
| 1096 if (defaultValue != null) { | 1076 if (defaultValue != null) { |
| 1097 ReferenceFinder parameterReferenceFinder = new ReferenceFinder( | 1077 ReferenceFinder parameterReferenceFinder = new ReferenceFinder( |
| 1098 parameter, referenceGraph, _variableDeclarationMap, | 1078 parameter, referenceGraph, _variableDeclarationMap, |
| 1099 constructorDeclarationMap); | 1079 constructorDeclarationMap); |
| 1100 defaultValue.accept(parameterReferenceFinder); | 1080 defaultValue.accept(parameterReferenceFinder); |
| 1101 } | 1081 } |
| 1102 } | 1082 } |
| 1103 } | 1083 } |
| 1104 }); | 1084 }); |
| 1105 for (InstanceCreationExpression expression in _constructorInvocations) { | |
| 1106 referenceGraph.addNode(expression); | |
| 1107 ConstructorElement constructor = expression.staticElement; | |
| 1108 if (constructor == null) { | |
| 1109 continue; | |
| 1110 } | |
| 1111 ConstructorDeclaration declaration = | |
| 1112 findConstructorDeclaration(constructor); | |
| 1113 // An instance creation expression depends both on the constructor and | |
| 1114 // the arguments passed to it. | |
| 1115 ReferenceFinder referenceFinder = new ReferenceFinder(expression, | |
| 1116 referenceGraph, _variableDeclarationMap, constructorDeclarationMap); | |
| 1117 if (declaration != null) { | |
| 1118 referenceGraph.addEdge(expression, declaration); | |
| 1119 } | |
| 1120 expression.argumentList.accept(referenceFinder); | |
| 1121 } | |
| 1122 List<List<AstNode>> topologicalSort = | 1085 List<List<AstNode>> topologicalSort = |
| 1123 referenceGraph.computeTopologicalSort(); | 1086 referenceGraph.computeTopologicalSort(); |
| 1124 for (List<AstNode> constantsInCycle in topologicalSort) { | 1087 for (List<AstNode> constantsInCycle in topologicalSort) { |
| 1125 if (constantsInCycle.length == 1) { | 1088 if (constantsInCycle.length == 1) { |
| 1126 _computeValueFor(constantsInCycle[0]); | 1089 _computeValueFor(constantsInCycle[0]); |
| 1127 } else { | 1090 } else { |
| 1128 for (AstNode constant in constantsInCycle) { | 1091 for (AstNode constant in constantsInCycle) { |
| 1129 _generateCycleError(constantsInCycle, constant); | 1092 _generateCycleError(constantsInCycle, constant); |
| 1130 } | 1093 } |
| 1131 } | 1094 } |
| 1132 } | 1095 } |
| 1133 // Since no constant can depend on an annotation, we don't waste time | 1096 // Since no constant can depend on an annotation, we don't waste time |
| 1134 // including them in the topological sort. We just process all the | 1097 // including them in the topological sort. We just process all the |
| 1135 // annotations after all other constants are finished. | 1098 // annotations after all other constants are finished. |
| 1136 for (Annotation annotation in _annotations) { | 1099 for (Annotation annotation in _annotations) { |
| 1137 _computeValueFor(annotation); | 1100 _computeValueFor(annotation); |
| 1138 } | 1101 } |
| 1139 } | 1102 } |
| 1140 | 1103 |
| 1141 ConstructorDeclaration findConstructorDeclaration( | 1104 ConstructorDeclaration findConstructorDeclaration( |
| 1142 ConstructorElement constructor) => constructorDeclarationMap[ | 1105 ConstructorElement constructor) => constructorDeclarationMap[ |
| 1143 evaluationEngine._getConstructorBase(constructor)]; | 1106 ConstantEvaluationEngine._getConstructorBase(constructor)]; |
| 1144 | 1107 |
| 1145 VariableDeclaration findVariableDeclaration( | 1108 VariableDeclaration findVariableDeclaration( |
| 1146 PotentiallyConstVariableElement variable) => | 1109 PotentiallyConstVariableElement variable) => |
| 1147 _variableDeclarationMap[variable]; | 1110 _variableDeclarationMap[variable]; |
| 1148 | 1111 |
| 1149 /** | 1112 /** |
| 1150 * Compute a value for the given [constNode]. | 1113 * Compute a value for the given [constNode]. |
| 1151 */ | 1114 */ |
| 1152 void _computeValueFor(AstNode constNode) { | 1115 void _computeValueFor(AstNode constNode) { |
| 1153 evaluationEngine.validator.beforeComputeValue(constNode); | 1116 evaluationEngine.validator.beforeComputeValue(constNode); |
| 1154 if (constNode is VariableDeclaration) { | 1117 if (constNode is VariableDeclaration) { |
| 1155 VariableElement element = constNode.element; | 1118 VariableElement element = constNode.element; |
| 1156 RecordingErrorListener errorListener = new RecordingErrorListener(); | 1119 RecordingErrorListener errorListener = new RecordingErrorListener(); |
| 1157 ErrorReporter errorReporter = | 1120 ErrorReporter errorReporter = |
| 1158 new ErrorReporter(errorListener, element.source); | 1121 new ErrorReporter(errorListener, element.source); |
| 1159 DartObjectImpl dartObject = | 1122 DartObjectImpl dartObject = |
| 1160 (element as PotentiallyConstVariableElement).constantInitializer | 1123 (element as PotentiallyConstVariableElement).constantInitializer |
| 1161 .accept(new ConstantVisitor(evaluationEngine, errorReporter)); | 1124 .accept(new ConstantVisitor(evaluationEngine, errorReporter)); |
| 1162 if (dartObject != null) { | 1125 // Only check the type for truly const declarations (don't check final |
| 1126 // fields with initializers, since their types may be generic. The type | |
| 1127 // of the final field will be checked later, when the constructor is | |
| 1128 // invoked). | |
| 1129 if (dartObject != null && constNode.isConst) { | |
| 1163 if (!evaluationEngine.runtimeTypeMatch(dartObject, element.type)) { | 1130 if (!evaluationEngine.runtimeTypeMatch(dartObject, element.type)) { |
| 1164 errorReporter.reportErrorForElement( | 1131 errorReporter.reportErrorForElement( |
| 1165 CheckedModeCompileTimeErrorCode.VARIABLE_TYPE_MISMATCH, element, [ | 1132 CheckedModeCompileTimeErrorCode.VARIABLE_TYPE_MISMATCH, element, [ |
| 1166 dartObject.type, | 1133 dartObject.type, |
| 1167 element.type | 1134 element.type |
| 1168 ]); | 1135 ]); |
| 1169 } | 1136 } |
| 1170 } | 1137 } |
| 1171 (element as VariableElementImpl).evaluationResult = | 1138 (element as VariableElementImpl).evaluationResult = |
| 1172 new EvaluationResultImpl.con2(dartObject, errorListener.errors); | 1139 new EvaluationResultImpl.con2(dartObject, errorListener.errors); |
| 1173 } else if (constNode is InstanceCreationExpression) { | |
| 1174 InstanceCreationExpression expression = constNode; | |
| 1175 ConstructorElement constructor = expression.staticElement; | |
| 1176 if (constructor == null) { | |
| 1177 // Couldn't resolve the constructor so we can't compute a value. | |
| 1178 // No problem - the error has already been reported. | |
| 1179 // But we still need to store an evaluation result. | |
| 1180 expression.constantHandle.evaluationResult = | |
| 1181 new EvaluationResultImpl.con1(null); | |
| 1182 return; | |
| 1183 } | |
| 1184 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 1185 CompilationUnit sourceCompilationUnit = | |
| 1186 expression.getAncestor((node) => node is CompilationUnit); | |
| 1187 ErrorReporter errorReporter = new ErrorReporter( | |
| 1188 errorListener, sourceCompilationUnit.element.source); | |
| 1189 ConstantVisitor constantVisitor = | |
| 1190 new ConstantVisitor(evaluationEngine, errorReporter); | |
| 1191 DartObjectImpl result = evaluationEngine.evaluateConstructorCall( | |
| 1192 constNode, expression.argumentList.arguments, constructor, | |
| 1193 constantVisitor, errorReporter); | |
| 1194 expression.constantHandle.evaluationResult = | |
| 1195 new EvaluationResultImpl.con2(result, errorListener.errors); | |
| 1196 } else if (constNode is ConstructorDeclaration) { | 1140 } else if (constNode is ConstructorDeclaration) { |
| 1197 // No evaluation needs to be done; constructor declarations are only in | 1141 // No evaluation needs to be done; constructor declarations are only in |
| 1198 // the dependency graph to ensure that any constants referred to in | 1142 // the dependency graph to ensure that any constants referred to in |
| 1199 // initializer lists and parameter defaults are evaluated before | 1143 // initializer lists and parameter defaults are evaluated before |
| 1200 // invocations of the constructor. However we do need to annotate the | 1144 // invocations of the constructor. However we do need to annotate the |
| 1201 // element as being free of constant evaluation cycles so that later code | 1145 // element as being free of constant evaluation cycles so that later code |
| 1202 // will know that it is safe to evaluate. | 1146 // will know that it is safe to evaluate. |
| 1203 ConstructorElementImpl constructor = constNode.element; | 1147 ConstructorElementImpl constructor = constNode.element; |
| 1204 constructor.isCycleFree = true; | 1148 constructor.isCycleFree = true; |
| 1205 } else if (constNode is FormalParameter) { | 1149 } else if (constNode is FormalParameter) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1254 } | 1198 } |
| 1255 } else { | 1199 } else { |
| 1256 // Should not happen. | 1200 // Should not happen. |
| 1257 AnalysisEngine.instance.logger.logError( | 1201 AnalysisEngine.instance.logger.logError( |
| 1258 "Constant value computer trying to compute the value of a node which i s not a VariableDeclaration, InstanceCreationExpression, FormalParameter, or Con structorDeclaration"); | 1202 "Constant value computer trying to compute the value of a node which i s not a VariableDeclaration, InstanceCreationExpression, FormalParameter, or Con structorDeclaration"); |
| 1259 return; | 1203 return; |
| 1260 } | 1204 } |
| 1261 } | 1205 } |
| 1262 | 1206 |
| 1263 /** | 1207 /** |
| 1264 * Generate an error indicating that the given [constant] is not a valid | 1208 * Generate an error indicating that the given [constNode] is not a valid |
| 1265 * compile-time constant because it references at least one of the constants | 1209 * compile-time constant because it references at least one of the constants |
| 1266 * in the given [cycle], each of which directly or indirectly references the | 1210 * in the given [cycle], each of which directly or indirectly references the |
| 1267 * constant. | 1211 * constant. |
| 1268 */ | 1212 */ |
| 1269 void _generateCycleError(List<AstNode> cycle, AstNode constant) { | 1213 void _generateCycleError(List<AstNode> cycle, AstNode constNode) { |
| 1270 // TODO(brianwilkerson) Implement this. | 1214 if (constNode is VariableDeclaration) { |
| 1215 VariableElement element = constNode.element; | |
| 1216 RecordingErrorListener errorListener = new RecordingErrorListener(); | |
| 1217 ErrorReporter errorReporter = | |
| 1218 new ErrorReporter(errorListener, element.source); | |
| 1219 errorReporter.reportErrorForElement( | |
| 1220 CompileTimeErrorCode.RECURSIVE_COMPILE_TIME_CONSTANT, element, []); | |
|
Brian Wilkerson
2015/05/05 21:25:24
It would be really nice if we could extract enough
Paul Berry
2015/05/05 21:46:31
Done.
| |
| 1221 (element as VariableElementImpl).evaluationResult = | |
| 1222 new EvaluationResultImpl.con2(null, errorListener.errors); | |
| 1223 } else if (constNode is ConstructorDeclaration) { | |
| 1224 // We don't report cycle errors on constructor declarations since there | |
| 1225 // is nowhere to put the error information. | |
| 1226 } else if (constNode is FormalParameter) { | |
| 1227 // Formal parameter defaults should never appear as part of a cycle | |
| 1228 // because they can't be referred to. | |
| 1229 assert(false); | |
| 1230 } else if (constNode is Annotation) { | |
| 1231 // Annotations should never appear as part of a cycle because they can't | |
| 1232 // be referred to. | |
| 1233 assert(false); | |
| 1234 } else { | |
| 1235 // Should not happen. | |
| 1236 AnalysisEngine.instance.logger.logError( | |
| 1237 "Constant value computer trying to report a cycle error for an unexpec ted node"); | |
|
Brian Wilkerson
2015/05/05 21:25:24
It would be useful for the error message to includ
Paul Berry
2015/05/05 21:46:31
Done.
| |
| 1238 return; | |
|
Brian Wilkerson
2015/05/05 21:25:24
Not sure why we have an explicit return here.
Not
Paul Berry
2015/05/05 21:46:31
Done.
| |
| 1239 } | |
| 1271 } | 1240 } |
| 1272 } | 1241 } |
| 1273 | 1242 |
| 1274 /** | 1243 /** |
| 1275 * A visitor used to evaluate constant expressions to produce their compile-time | 1244 * A visitor used to evaluate constant expressions to produce their compile-time |
| 1276 * value. According to the Dart Language Specification: <blockquote> A constant | 1245 * value. According to the Dart Language Specification: <blockquote> A constant |
| 1277 * expression is one of the following: | 1246 * expression is one of the following: |
| 1278 * | 1247 * |
| 1279 * * A literal number. | 1248 * * A literal number. |
| 1280 * * A literal boolean. | 1249 * * A literal boolean. |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1486 new DartObjectImpl(_typeProvider.doubleType, new DoubleState(node.value)); | 1455 new DartObjectImpl(_typeProvider.doubleType, new DoubleState(node.value)); |
| 1487 | 1456 |
| 1488 @override | 1457 @override |
| 1489 DartObjectImpl visitInstanceCreationExpression( | 1458 DartObjectImpl visitInstanceCreationExpression( |
| 1490 InstanceCreationExpression node) { | 1459 InstanceCreationExpression node) { |
| 1491 if (!node.isConst) { | 1460 if (!node.isConst) { |
| 1492 // TODO(brianwilkerson) Figure out which error to report. | 1461 // TODO(brianwilkerson) Figure out which error to report. |
| 1493 _error(node, null); | 1462 _error(node, null); |
| 1494 return null; | 1463 return null; |
| 1495 } | 1464 } |
| 1496 evaluationEngine.validator.beforeGetEvaluationResult(node); | 1465 ConstructorElement constructor = node.staticElement; |
| 1497 EvaluationResultImpl result = node.evaluationResult; | 1466 if (constructor == null) { |
| 1498 if (result != null) { | 1467 // Couldn't resolve the constructor so we can't compute a value. No |
| 1499 return result.value; | 1468 // problem - the error has already been reported. |
| 1469 return null; | |
| 1500 } | 1470 } |
| 1501 // TODO(brianwilkerson) Figure out which error to report. | 1471 return evaluationEngine.evaluateConstructorCall( |
| 1502 _error(node, null); | 1472 node, node.argumentList.arguments, constructor, this, _errorReporter); |
| 1503 return null; | |
| 1504 } | 1473 } |
| 1505 | 1474 |
| 1506 @override | 1475 @override |
| 1507 DartObjectImpl visitIntegerLiteral(IntegerLiteral node) => | 1476 DartObjectImpl visitIntegerLiteral(IntegerLiteral node) => |
| 1508 new DartObjectImpl(_typeProvider.intType, new IntState(node.value)); | 1477 new DartObjectImpl(_typeProvider.intType, new IntState(node.value)); |
| 1509 | 1478 |
| 1510 @override | 1479 @override |
| 1511 DartObjectImpl visitInterpolationExpression(InterpolationExpression node) { | 1480 DartObjectImpl visitInterpolationExpression(InterpolationExpression node) { |
| 1512 DartObjectImpl result = node.expression.accept(this); | 1481 DartObjectImpl result = node.expression.accept(this); |
| 1513 if (result != null && !result.isBoolNumStringOrNull) { | 1482 if (result != null && !result.isBoolNumStringOrNull) { |
| (...skipping 3424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4938 * The [source] is the element representing the variable whose initializer | 4907 * The [source] is the element representing the variable whose initializer |
| 4939 * will be visited. The [referenceGraph] is a graph recording which variables | 4908 * will be visited. The [referenceGraph] is a graph recording which variables |
| 4940 * (heads) reference which other variables (tails) in their initializers. The | 4909 * (heads) reference which other variables (tails) in their initializers. The |
| 4941 * [variableDeclarationMap] is a table mapping constant variables to the | 4910 * [variableDeclarationMap] is a table mapping constant variables to the |
| 4942 * declarations of those variables. The [constructorDeclarationMap] is a table | 4911 * declarations of those variables. The [constructorDeclarationMap] is a table |
| 4943 * mapping constant constructors to the declarations of those constructors. | 4912 * mapping constant constructors to the declarations of those constructors. |
| 4944 */ | 4913 */ |
| 4945 ReferenceFinder(this._source, this._referenceGraph, | 4914 ReferenceFinder(this._source, this._referenceGraph, |
| 4946 this._variableDeclarationMap, this._constructorDeclarationMap); | 4915 this._variableDeclarationMap, this._constructorDeclarationMap); |
| 4947 | 4916 |
| 4917 ConstructorDeclaration findConstructorDeclaration( | |
| 4918 ConstructorElement constructor) => _constructorDeclarationMap[ | |
| 4919 ConstantEvaluationEngine._getConstructorBase(constructor)]; | |
| 4920 | |
| 4948 @override | 4921 @override |
| 4949 Object visitInstanceCreationExpression(InstanceCreationExpression node) { | 4922 Object visitInstanceCreationExpression(InstanceCreationExpression node) { |
| 4950 if (node.isConst) { | 4923 if (node.isConst) { |
| 4951 _referenceGraph.addEdge(_source, node); | 4924 ConstructorElement constructor = node.staticElement; |
| 4925 if (constructor != null) { | |
| 4926 ConstructorDeclaration declaration = | |
| 4927 findConstructorDeclaration(constructor); | |
| 4928 _referenceGraph.addEdge(_source, declaration); | |
| 4929 } | |
| 4952 } | 4930 } |
| 4953 return null; | 4931 return super.visitInstanceCreationExpression(node); |
| 4954 } | 4932 } |
| 4955 | 4933 |
| 4956 @override | 4934 @override |
| 4957 Object visitRedirectingConstructorInvocation( | 4935 Object visitRedirectingConstructorInvocation( |
| 4958 RedirectingConstructorInvocation node) { | 4936 RedirectingConstructorInvocation node) { |
| 4959 super.visitRedirectingConstructorInvocation(node); | 4937 super.visitRedirectingConstructorInvocation(node); |
| 4960 ConstructorElement target = node.staticElement; | 4938 ConstructorElement target = node.staticElement; |
| 4961 if (target != null && target.isConst) { | 4939 if (target != null && target.isConst) { |
| 4962 ConstructorDeclaration targetDeclaration = | 4940 ConstructorDeclaration targetDeclaration = |
| 4963 _constructorDeclarationMap[target]; | 4941 _constructorDeclarationMap[target]; |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 5220 return BoolState.from(_element == rightElement); | 5198 return BoolState.from(_element == rightElement); |
| 5221 } else if (rightOperand is DynamicState) { | 5199 } else if (rightOperand is DynamicState) { |
| 5222 return BoolState.UNKNOWN_VALUE; | 5200 return BoolState.UNKNOWN_VALUE; |
| 5223 } | 5201 } |
| 5224 return BoolState.FALSE_STATE; | 5202 return BoolState.FALSE_STATE; |
| 5225 } | 5203 } |
| 5226 | 5204 |
| 5227 @override | 5205 @override |
| 5228 String toString() => _element == null ? "-unknown-" : _element.name; | 5206 String toString() => _element == null ? "-unknown-" : _element.name; |
| 5229 } | 5207 } |
| OLD | NEW |