| 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 library analyzer.src.dart.constant.evaluation; | 5 library analyzer.src.dart.constant.evaluation; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/context/declared_variables.dart'; | 9 import 'package:analyzer/context/declared_variables.dart'; |
| 10 import 'package:analyzer/dart/ast/ast.dart'; | 10 import 'package:analyzer/dart/ast/ast.dart'; |
| 11 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; |
| 11 import 'package:analyzer/dart/ast/token.dart'; | 12 import 'package:analyzer/dart/ast/token.dart'; |
| 12 import 'package:analyzer/dart/ast/visitor.dart'; | 13 import 'package:analyzer/dart/ast/visitor.dart'; |
| 13 import 'package:analyzer/dart/constant/value.dart'; | 14 import 'package:analyzer/dart/constant/value.dart'; |
| 14 import 'package:analyzer/dart/element/element.dart'; | 15 import 'package:analyzer/dart/element/element.dart'; |
| 15 import 'package:analyzer/dart/element/type.dart'; | 16 import 'package:analyzer/dart/element/type.dart'; |
| 16 import 'package:analyzer/error/error.dart'; | 17 import 'package:analyzer/error/error.dart'; |
| 17 import 'package:analyzer/error/listener.dart'; | 18 import 'package:analyzer/error/listener.dart'; |
| 18 import 'package:analyzer/src/dart/constant/utilities.dart'; | 19 import 'package:analyzer/src/dart/constant/utilities.dart'; |
| 19 import 'package:analyzer/src/dart/constant/value.dart'; | 20 import 'package:analyzer/src/dart/constant/value.dart'; |
| 20 import 'package:analyzer/src/dart/element/element.dart'; | 21 import 'package:analyzer/src/dart/element/element.dart'; |
| 21 import 'package:analyzer/src/dart/element/member.dart'; | 22 import 'package:analyzer/src/dart/element/member.dart'; |
| 22 import 'package:analyzer/src/error/codes.dart'; | 23 import 'package:analyzer/src/error/codes.dart'; |
| 23 import 'package:analyzer/src/generated/engine.dart'; | 24 import 'package:analyzer/src/generated/engine.dart'; |
| 24 import 'package:analyzer/src/generated/engine.dart' | 25 import 'package:analyzer/src/generated/engine.dart' |
| 25 show AnalysisEngine, RecordingErrorListener; | 26 show AnalysisEngine, RecordingErrorListener; |
| 26 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; | 27 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
| 27 import 'package:analyzer/src/generated/type_system.dart' | 28 import 'package:analyzer/src/generated/type_system.dart' |
| 28 show TypeSystem, TypeSystemImpl; | 29 show TypeSystem, TypeSystemImpl; |
| 29 import 'package:analyzer/src/generated/utilities_collection.dart'; | 30 import 'package:analyzer/src/generated/utilities_collection.dart'; |
| 30 import 'package:analyzer/src/generated/utilities_dart.dart' show ParameterKind; | 31 import 'package:analyzer/src/generated/utilities_dart.dart' show ParameterKind; |
| 31 import 'package:analyzer/src/task/dart.dart'; | 32 import 'package:analyzer/src/task/dart.dart'; |
| 32 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; | |
| 33 | 33 |
| 34 /** | 34 /** |
| 35 * Helper class encapsulating the methods for evaluating constants and | 35 * Helper class encapsulating the methods for evaluating constants and |
| 36 * constant instance creation expressions. | 36 * constant instance creation expressions. |
| 37 */ | 37 */ |
| 38 class ConstantEvaluationEngine { | 38 class ConstantEvaluationEngine { |
| 39 /** | 39 /** |
| 40 * Parameter to "fromEnvironment" methods that denotes the default value. | 40 * Parameter to "fromEnvironment" methods that denotes the default value. |
| 41 */ | 41 */ |
| 42 static String _DEFAULT_VALUE_PARAM = "defaultValue"; | 42 static String _DEFAULT_VALUE_PARAM = "defaultValue"; |
| (...skipping 1244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1287 } | 1287 } |
| 1288 if (conditionResult.toBoolValue() == true) { | 1288 if (conditionResult.toBoolValue() == true) { |
| 1289 return thenResult; | 1289 return thenResult; |
| 1290 } else if (conditionResult.toBoolValue() == false) { | 1290 } else if (conditionResult.toBoolValue() == false) { |
| 1291 return elseResult; | 1291 return elseResult; |
| 1292 } | 1292 } |
| 1293 ParameterizedType thenType = thenResult.type; | 1293 ParameterizedType thenType = thenResult.type; |
| 1294 ParameterizedType elseType = elseResult.type; | 1294 ParameterizedType elseType = elseResult.type; |
| 1295 return new DartObjectImpl.validWithUnknownValue( | 1295 return new DartObjectImpl.validWithUnknownValue( |
| 1296 _typeSystem.getLeastUpperBound(_typeProvider, thenType, elseType) | 1296 _typeSystem.getLeastUpperBound(_typeProvider, thenType, elseType) |
| 1297 as InterfaceType); | 1297 as InterfaceType); |
| 1298 } | 1298 } |
| 1299 | 1299 |
| 1300 @override | 1300 @override |
| 1301 DartObjectImpl visitDoubleLiteral(DoubleLiteral node) => | 1301 DartObjectImpl visitDoubleLiteral(DoubleLiteral node) => |
| 1302 new DartObjectImpl(_typeProvider.doubleType, new DoubleState(node.value)); | 1302 new DartObjectImpl(_typeProvider.doubleType, new DoubleState(node.value)); |
| 1303 | 1303 |
| 1304 @override | 1304 @override |
| 1305 DartObjectImpl visitInstanceCreationExpression( | 1305 DartObjectImpl visitInstanceCreationExpression( |
| 1306 InstanceCreationExpression node) { | 1306 InstanceCreationExpression node) { |
| 1307 if (!node.isConst) { | 1307 if (!node.isConst) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1352 if (elementResult == null) { | 1352 if (elementResult == null) { |
| 1353 errorOccurred = true; | 1353 errorOccurred = true; |
| 1354 } else { | 1354 } else { |
| 1355 elements.add(elementResult); | 1355 elements.add(elementResult); |
| 1356 } | 1356 } |
| 1357 } | 1357 } |
| 1358 if (errorOccurred) { | 1358 if (errorOccurred) { |
| 1359 return null; | 1359 return null; |
| 1360 } | 1360 } |
| 1361 DartType elementType = _typeProvider.dynamicType; | 1361 DartType elementType = _typeProvider.dynamicType; |
| 1362 NodeList<TypeName> typeArgs = node.typeArguments?.arguments; | 1362 NodeList<TypeAnnotation> typeArgs = node.typeArguments?.arguments; |
| 1363 if (typeArgs?.length == 1) { | 1363 if (typeArgs?.length == 1) { |
| 1364 DartType type = visitTypeName(typeArgs[0])?.toTypeValue(); | 1364 DartType type = visitTypeAnnotation(typeArgs[0])?.toTypeValue(); |
| 1365 if (type != null) { | 1365 if (type != null) { |
| 1366 elementType = type; | 1366 elementType = type; |
| 1367 } | 1367 } |
| 1368 } | 1368 } |
| 1369 InterfaceType listType = _typeProvider.listType.instantiate([elementType]); | 1369 InterfaceType listType = _typeProvider.listType.instantiate([elementType]); |
| 1370 return new DartObjectImpl(listType, new ListState(elements)); | 1370 return new DartObjectImpl(listType, new ListState(elements)); |
| 1371 } | 1371 } |
| 1372 | 1372 |
| 1373 @override | 1373 @override |
| 1374 DartObjectImpl visitMapLiteral(MapLiteral node) { | 1374 DartObjectImpl visitMapLiteral(MapLiteral node) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1387 errorOccurred = true; | 1387 errorOccurred = true; |
| 1388 } else { | 1388 } else { |
| 1389 map[keyResult] = valueResult; | 1389 map[keyResult] = valueResult; |
| 1390 } | 1390 } |
| 1391 } | 1391 } |
| 1392 if (errorOccurred) { | 1392 if (errorOccurred) { |
| 1393 return null; | 1393 return null; |
| 1394 } | 1394 } |
| 1395 DartType keyType = _typeProvider.dynamicType; | 1395 DartType keyType = _typeProvider.dynamicType; |
| 1396 DartType valueType = _typeProvider.dynamicType; | 1396 DartType valueType = _typeProvider.dynamicType; |
| 1397 NodeList<TypeName> typeArgs = node.typeArguments?.arguments; | 1397 NodeList<TypeAnnotation> typeArgs = node.typeArguments?.arguments; |
| 1398 if (typeArgs?.length == 2) { | 1398 if (typeArgs?.length == 2) { |
| 1399 DartType keyTypeCandidate = visitTypeName(typeArgs[0])?.toTypeValue(); | 1399 DartType keyTypeCandidate = |
| 1400 visitTypeAnnotation(typeArgs[0])?.toTypeValue(); |
| 1400 if (keyTypeCandidate != null) { | 1401 if (keyTypeCandidate != null) { |
| 1401 keyType = keyTypeCandidate; | 1402 keyType = keyTypeCandidate; |
| 1402 } | 1403 } |
| 1403 DartType valueTypeCandidate = visitTypeName(typeArgs[1])?.toTypeValue(); | 1404 DartType valueTypeCandidate = |
| 1405 visitTypeAnnotation(typeArgs[1])?.toTypeValue(); |
| 1404 if (valueTypeCandidate != null) { | 1406 if (valueTypeCandidate != null) { |
| 1405 valueType = valueTypeCandidate; | 1407 valueType = valueTypeCandidate; |
| 1406 } | 1408 } |
| 1407 } | 1409 } |
| 1408 InterfaceType mapType = | 1410 InterfaceType mapType = |
| 1409 _typeProvider.mapType.instantiate([keyType, valueType]); | 1411 _typeProvider.mapType.instantiate([keyType, valueType]); |
| 1410 return new DartObjectImpl(mapType, new MapState(map)); | 1412 return new DartObjectImpl(mapType, new MapState(map)); |
| 1411 } | 1413 } |
| 1412 | 1414 |
| 1413 @override | 1415 @override |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1543 for (int i = 0; i < components.length; i++) { | 1545 for (int i = 0; i < components.length; i++) { |
| 1544 if (i > 0) { | 1546 if (i > 0) { |
| 1545 buffer.writeCharCode(0x2E); | 1547 buffer.writeCharCode(0x2E); |
| 1546 } | 1548 } |
| 1547 buffer.write(components[i].lexeme); | 1549 buffer.write(components[i].lexeme); |
| 1548 } | 1550 } |
| 1549 return new DartObjectImpl( | 1551 return new DartObjectImpl( |
| 1550 _typeProvider.symbolType, new SymbolState(buffer.toString())); | 1552 _typeProvider.symbolType, new SymbolState(buffer.toString())); |
| 1551 } | 1553 } |
| 1552 | 1554 |
| 1553 @override | 1555 DartObjectImpl visitTypeAnnotation(TypeAnnotation node) { |
| 1554 DartObjectImpl visitTypeName(TypeName node) { | |
| 1555 DartType type = evaluateType(node.type); | 1556 DartType type = evaluateType(node.type); |
| 1556 if (type == null) { | 1557 if (type == null) { |
| 1557 return super.visitTypeName(node); | 1558 return super.visitTypeName(node); |
| 1558 } | 1559 } |
| 1559 return typeConstant(type); | 1560 return typeConstant(type); |
| 1560 } | 1561 } |
| 1561 | 1562 |
| 1563 @override |
| 1564 DartObjectImpl visitTypeName(TypeName node) => visitTypeAnnotation(node); |
| 1565 |
| 1562 /** | 1566 /** |
| 1563 * Create an error associated with the given [node]. The error will have the | 1567 * Create an error associated with the given [node]. The error will have the |
| 1564 * given error [code]. | 1568 * given error [code]. |
| 1565 */ | 1569 */ |
| 1566 void _error(AstNode node, ErrorCode code) { | 1570 void _error(AstNode node, ErrorCode code) { |
| 1567 _errorReporter.reportErrorForNode( | 1571 _errorReporter.reportErrorForNode( |
| 1568 code ?? CompileTimeErrorCode.INVALID_CONSTANT, node); | 1572 code ?? CompileTimeErrorCode.INVALID_CONSTANT, node); |
| 1569 } | 1573 } |
| 1570 | 1574 |
| 1571 /** | 1575 /** |
| (...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2081 } | 2085 } |
| 2082 | 2086 |
| 2083 @override | 2087 @override |
| 2084 String toString() { | 2088 String toString() { |
| 2085 if (value == null) { | 2089 if (value == null) { |
| 2086 return "error"; | 2090 return "error"; |
| 2087 } | 2091 } |
| 2088 return value.toString(); | 2092 return value.toString(); |
| 2089 } | 2093 } |
| 2090 } | 2094 } |
| OLD | NEW |