| 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 package com.google.dart.compiler.resolver; | 5 package com.google.dart.compiler.resolver; |
| 6 | 6 |
| 7 import com.google.common.collect.Maps; | 7 import com.google.common.collect.Maps; |
| 8 import com.google.common.collect.Sets; | 8 import com.google.common.collect.Sets; |
| 9 import com.google.dart.compiler.DartCompilationError; | 9 import com.google.dart.compiler.DartCompilationError; |
| 10 import com.google.dart.compiler.DartCompilationPhase; | 10 import com.google.dart.compiler.DartCompilationPhase; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 import com.google.dart.compiler.ast.DartUnaryExpression; | 45 import com.google.dart.compiler.ast.DartUnaryExpression; |
| 46 import com.google.dart.compiler.ast.DartUnit; | 46 import com.google.dart.compiler.ast.DartUnit; |
| 47 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; | 47 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; |
| 48 import com.google.dart.compiler.ast.DartVariable; | 48 import com.google.dart.compiler.ast.DartVariable; |
| 49 import com.google.dart.compiler.ast.DartVariableStatement; | 49 import com.google.dart.compiler.ast.DartVariableStatement; |
| 50 import com.google.dart.compiler.ast.Modifiers; | 50 import com.google.dart.compiler.ast.Modifiers; |
| 51 import com.google.dart.compiler.common.HasSourceInfo; | 51 import com.google.dart.compiler.common.HasSourceInfo; |
| 52 import com.google.dart.compiler.type.Type; | 52 import com.google.dart.compiler.type.Type; |
| 53 import com.google.dart.compiler.type.TypeKind; | 53 import com.google.dart.compiler.type.TypeKind; |
| 54 | 54 |
| 55 import java.math.BigInteger; |
| 55 import java.util.List; | 56 import java.util.List; |
| 56 import java.util.Map; | 57 import java.util.Map; |
| 57 import java.util.Set; | 58 import java.util.Set; |
| 58 | 59 |
| 59 /** | 60 /** |
| 60 * Given an tree, finds all compile-time constant expressions, and determines if | 61 * Given an tree, finds all compile-time constant expressions, and determines if |
| 61 * each expression matches all the rules for a compile-time constant. Emits a | 62 * each expression matches all the rules for a compile-time constant. Emits a |
| 62 * resolution error if not. | 63 * resolution error if not. |
| 63 * | 64 * |
| 64 * This script doesn't just resolve expressions, it also sets types to the | 65 * This script doesn't just resolve expressions, it also sets types to the |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 * @return a non <code>null</code> type value. Dynamic if none other can be | 171 * @return a non <code>null</code> type value. Dynamic if none other can be |
| 171 * determined. | 172 * determined. |
| 172 */ | 173 */ |
| 173 private Type getMostSpecificType(DartNode node) { | 174 private Type getMostSpecificType(DartNode node) { |
| 174 if (node != null) { | 175 if (node != null) { |
| 175 Type type = inferredTypes.get(node); | 176 Type type = inferredTypes.get(node); |
| 176 if (type != null) { | 177 if (type != null) { |
| 177 return type; | 178 return type; |
| 178 } | 179 } |
| 179 Element element = node.getElement(); | 180 Element element = node.getElement(); |
| 181 if (Elements.isFunctionIdentical(element)) { |
| 182 return boolType; |
| 183 } |
| 180 if (element != null) { | 184 if (element != null) { |
| 181 type = element.getType(); | 185 type = element.getType(); |
| 182 if (type != null && TypeKind.of(type) != TypeKind.DYNAMIC) { | 186 if (type != null && TypeKind.of(type) != TypeKind.DYNAMIC) { |
| 183 return type; | 187 return type; |
| 184 } | 188 } |
| 185 if (element instanceof VariableElement) { | 189 if (element instanceof VariableElement) { |
| 186 VariableElement variable = (VariableElement) element; | 190 VariableElement variable = (VariableElement) element; |
| 187 if (variable.getModifiers().isConstant()) { | 191 if (variable.getModifiers().isConstant()) { |
| 188 DartExpression value = variable.getDefaultValue(); | 192 DartExpression value = variable.getDefaultValue(); |
| 189 if (value != null) { | 193 if (value != null) { |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 } | 271 } |
| 268 } else { | 272 } else { |
| 269 checkMathExpression(x, lhs, rhs, lhsType, rhsType); | 273 checkMathExpression(x, lhs, rhs, lhsType, rhsType); |
| 270 } | 274 } |
| 271 break; | 275 break; |
| 272 case SUB: | 276 case SUB: |
| 273 case MUL: | 277 case MUL: |
| 274 case DIV: | 278 case DIV: |
| 275 checkMathExpression(x, lhs, rhs, lhsType, rhsType); | 279 checkMathExpression(x, lhs, rhs, lhsType, rhsType); |
| 276 break; | 280 break; |
| 281 case TRUNC: |
| 282 reportExceptionIfZeroLiteral(x, rhs); |
| 283 // pass-through |
| 277 case MOD: | 284 case MOD: |
| 278 case TRUNC: | |
| 279 if (checkNumber(lhs, lhsType) && checkNumber(rhs, rhsType)) { | 285 if (checkNumber(lhs, lhsType) && checkNumber(rhs, rhsType)) { |
| 280 rememberInferredType(x, intType); | 286 rememberInferredType(x, intType); |
| 281 } | 287 } |
| 282 break; | 288 break; |
| 283 case LT: | 289 case LT: |
| 284 case GT: | 290 case GT: |
| 285 case LTE: | 291 case LTE: |
| 286 case GTE: | 292 case GTE: |
| 287 if (checkNumber(lhs, lhsType) && checkNumber(rhs, rhsType)) { | 293 if (checkNumber(lhs, lhsType) && checkNumber(rhs, rhsType)) { |
| 288 rememberInferredType(x, boolType); | 294 rememberInferredType(x, boolType); |
| 289 } | 295 } |
| 290 break; | 296 break; |
| 291 | 297 |
| 292 default: | 298 default: |
| 293 // all other operators... | 299 // all other operators... |
| 294 expectedConstant(x); | 300 expectedConstant(x); |
| 295 } | 301 } |
| 296 return null; | 302 return null; |
| 297 } | 303 } |
| 304 |
| 305 private void reportExceptionIfZeroLiteral(HasSourceInfo target, DartExpressi
on e) { |
| 306 if (e instanceof DartIntegerLiteral) { |
| 307 DartIntegerLiteral literal = (DartIntegerLiteral) e; |
| 308 if (literal.getValue().equals(BigInteger.ZERO)) { |
| 309 context.onError(new DartCompilationError(target, |
| 310 ResolverErrorCode.CONSTANTS_EVALUATION_EXCEPTION)); |
| 311 } |
| 312 } |
| 313 } |
| 298 | 314 |
| 299 private void checkMathExpression(DartBinaryExpression x, | 315 private void checkMathExpression(DartBinaryExpression x, |
| 300 DartExpression lhs, DartExpression rhs, | 316 DartExpression lhs, DartExpression rhs, |
| 301 Type lhsType, Type rhsType) { | 317 Type lhsType, Type rhsType) { |
| 302 if (checkNumber(lhs, lhsType) && checkNumber(rhs, rhsType)) { | 318 if (checkNumber(lhs, lhsType) && checkNumber(rhs, rhsType)) { |
| 303 if (lhsType.equals(intType) && rhsType.equals(intType)) { | 319 if (lhsType.equals(intType) && rhsType.equals(intType)) { |
| 304 rememberInferredType(x, intType); | 320 rememberInferredType(x, intType); |
| 305 } else if (lhsType.equals(doubleType) && rhsType.equals(doubleType)) { | 321 } else if (lhsType.equals(doubleType) && rhsType.equals(doubleType)) { |
| 306 rememberInferredType(x, doubleType); | 322 rememberInferredType(x, doubleType); |
| 307 } else if (lhsType.equals(doubleType) && rhsType.equals(intType) | 323 } else if (lhsType.equals(doubleType) && rhsType.equals(intType) |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 791 expression.accept(visitor); | 807 expression.accept(visitor); |
| 792 return visitor.getMostSpecificType(expression); | 808 return visitor.getMostSpecificType(expression); |
| 793 } | 809 } |
| 794 return null; | 810 return null; |
| 795 } | 811 } |
| 796 | 812 |
| 797 public void exec(DartUnit unit) { | 813 public void exec(DartUnit unit) { |
| 798 unit.accept(new FindCompileTimeConstantExpressionsVisitor()); | 814 unit.accept(new FindCompileTimeConstantExpressionsVisitor()); |
| 799 } | 815 } |
| 800 } | 816 } |
| OLD | NEW |