Use TypeSystemImpl instead of TypeProvider in DartObjectImpl.

We need to be able to use TypeSystemImpl in DartObjectImpl.== to
call runtimeTypesEqual() instead of DartType.==

Change-Id: I2021a0d86a7c304072fd8fee2d1a7e6e979100a8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/134146
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Konstantin Shcheglov
2020-02-03 20:38:46 +00:00
committed by commit-bot@chromium.org
parent a165b62561
commit 1fff623c19
21 changed files with 940 additions and 537 deletions
+3
View File
@@ -4,6 +4,9 @@
feature, type arguments derived from type parameter bounds cannot be used as
is, and might require erasing nullability, when the element is instantiated
from a legacy library. Use `TypeSystem.instantiateToBounds2()` instead.
* Deprecated `DeclaredVariables.getBool/getInt/getString()`. These methods
are used internally for constants computation, and should not be used by
clients.
## 0.39.4
* Deprecated `DartType.name`, use `element` or `getDisplayString()` instead.
@@ -4,7 +4,8 @@
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/src/dart/constant/value.dart';
import 'package:analyzer/src/dart/constant/from_environment_evaluator.dart';
import 'package:analyzer/src/generated/type_system.dart';
/// An object used to provide access to the values of variables that have been
/// defined on the command line using the `-D` option.
@@ -49,35 +50,18 @@ class DeclaredVariables {
/// DartObject representing "unknown" is returned. If the value cannot be
/// parsed as a boolean, a DartObject representing 'null' is returned. The
/// [typeProvider] is the type provider used to find the type 'bool'.
@Deprecated("Clients don't need this functionality")
DartObject getBool(TypeProvider typeProvider, String name) {
String value = _declaredVariables[name];
if (value == null) {
return DartObjectImpl(typeProvider.boolType, BoolState.UNKNOWN_VALUE);
}
if (value == "true") {
return DartObjectImpl(typeProvider.boolType, BoolState.TRUE_STATE);
} else if (value == "false") {
return DartObjectImpl(typeProvider.boolType, BoolState.FALSE_STATE);
}
return DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE);
return _evaluator(typeProvider).getBool(name);
}
/// Return the value of the variable with the given [name] interpreted as an
/// integer value. If the variable is not defined (or [name] is `null`), a
/// DartObject representing "unknown" is returned. If the value cannot be
/// parsed as an integer, a DartObject representing 'null' is returned.
@Deprecated("Clients don't need this functionality")
DartObject getInt(TypeProvider typeProvider, String name) {
String value = _declaredVariables[name];
if (value == null) {
return DartObjectImpl(typeProvider.intType, IntState.UNKNOWN_VALUE);
}
int bigInteger;
try {
bigInteger = int.parse(value);
} on FormatException {
return DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE);
}
return DartObjectImpl(typeProvider.intType, IntState(bigInteger));
return _evaluator(typeProvider).getInt(name);
}
/// Return the value of the variable with the given [name] interpreted as a
@@ -86,11 +70,18 @@ class DeclaredVariables {
/// variable is not defined (or [name] is `null`), a DartObject representing
/// "unknown" is returned. The [typeProvider] is the type provider used to
/// find the type 'String'.
@Deprecated("Clients don't need this functionality")
DartObject getString(TypeProvider typeProvider, String name) {
String value = _declaredVariables[name];
if (value == null) {
return DartObjectImpl(typeProvider.stringType, StringState.UNKNOWN_VALUE);
}
return DartObjectImpl(typeProvider.stringType, StringState(value));
return _evaluator(typeProvider).getString(name);
}
FromEnvironmentEvaluator _evaluator(TypeProvider typeProvider) {
var typeSystem = TypeSystemImpl(
implicitCasts: false,
isNonNullableByDefault: false,
strictInference: false,
typeProvider: typeProvider,
);
return FromEnvironmentEvaluator(typeSystem, this);
}
}
@@ -111,6 +111,7 @@ abstract class TypeProvider {
ClassElement get nullElement;
/// Return a [DartObjectImpl] representing the `null` object.
@deprecated
DartObjectImpl get nullObject;
/// Return the type representing the built-in type 'Null'.
@@ -236,7 +236,7 @@ class LibraryAnalyzer {
void _computeConstantErrors(
ErrorReporter errorReporter, CompilationUnit unit) {
ConstantVerifier constantVerifier = ConstantVerifier(
errorReporter, _libraryElement, _typeProvider, _declaredVariables,
errorReporter, _libraryElement, _declaredVariables,
featureSet: unit.featureSet, forAnalysisDriver: true);
unit.accept(constantVerifier);
}
@@ -11,7 +11,6 @@ import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/dart/element/type_system.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
@@ -22,6 +21,7 @@ import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/type_system.dart';
/// Instances of the class `ConstantVerifier` traverse an AST structure looking
/// for additional errors and warnings not covered by the parser and resolver.
@@ -31,6 +31,9 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
/// The error reporter by which errors will be reported.
final ErrorReporter _errorReporter;
/// The type operations.
final TypeSystemImpl _typeSystem;
/// The type provider used to access the known types.
final TypeProvider _typeProvider;
@@ -51,7 +54,7 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
/// Initialize a newly created constant verifier.
ConstantVerifier(ErrorReporter errorReporter, LibraryElement currentLibrary,
TypeProvider typeProvider, DeclaredVariables declaredVariables,
DeclaredVariables declaredVariables,
// TODO(brianwilkerson) Remove the unused parameter `forAnalysisDriver`.
{bool forAnalysisDriver,
// TODO(paulberry): make [featureSet] a required parameter.
@@ -59,9 +62,9 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
: this._(
errorReporter,
currentLibrary,
typeProvider,
declaredVariables,
currentLibrary.typeSystem,
currentLibrary.typeProvider,
declaredVariables,
featureSet ??
(currentLibrary.context.analysisOptions as AnalysisOptionsImpl)
.contextFeatures);
@@ -69,16 +72,16 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
ConstantVerifier._(
this._errorReporter,
this._currentLibrary,
this._typeSystem,
this._typeProvider,
this.declaredVariables,
TypeSystem typeSystem,
FeatureSet featureSet)
: _constantUpdate2018Enabled =
featureSet.isEnabled(Feature.constant_update_2018),
_intType = _typeProvider.intType,
_evaluationEngine = ConstantEvaluationEngine(
_typeProvider, declaredVariables,
typeSystem: typeSystem, experimentStatus: featureSet);
typeSystem: _typeSystem, experimentStatus: featureSet);
@override
void visitAnnotation(Annotation node) {
@@ -515,7 +518,11 @@ class ConstantVerifier extends RecursiveAstVisitor<void> {
Expression defaultValue = parameter.defaultValue;
DartObjectImpl result;
if (defaultValue == null) {
result = DartObjectImpl(_typeProvider.nullType, NullState.NULL_STATE);
result = DartObjectImpl(
_typeSystem,
_typeProvider.nullType,
NullState.NULL_STATE,
);
} else {
result = _validate(
defaultValue, CompileTimeErrorCode.NON_CONSTANT_DEFAULT_VALUE);
@@ -18,6 +18,7 @@ import 'package:analyzer/dart/element/type_system.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/src/dart/analysis/experiments.dart';
import 'package:analyzer/src/dart/constant/from_environment_evaluator.dart';
import 'package:analyzer/src/dart/constant/potentially_constant.dart';
import 'package:analyzer/src/dart/constant/utilities.dart';
import 'package:analyzer/src/dart/constant/value.dart';
@@ -67,8 +68,9 @@ class ConstantEvaluationEngine {
/// exact value is unknown.
final TypeSystem typeSystem;
/// The set of variables declared on the command line using '-D'.
final DeclaredVariables _declaredVariables;
/// The helper for evaluating variables declared on the command line
/// using '-D', and represented as [DeclaredVariables].
FromEnvironmentEvaluator _fromEnvironmentEvaluator;
/// Return the object representing the state of active experiments.
final ExperimentStatus experimentStatus;
@@ -78,11 +80,12 @@ class ConstantEvaluationEngine {
final ConstantEvaluationValidator validator;
/// Initialize a newly created [ConstantEvaluationEngine]. The [typeProvider]
/// is used to access known types. [_declaredVariables] is the set of
/// is used to access known types. [_fromEnvironmentEvaluator] is the set of
/// variables declared on the command line using '-D'. The [validator], if
/// given, is used to verify correct dependency analysis when running unit
/// tests.
ConstantEvaluationEngine(TypeProvider typeProvider, this._declaredVariables,
ConstantEvaluationEngine(
TypeProvider typeProvider, DeclaredVariables declaredVariables,
{ConstantEvaluationValidator validator,
ExperimentStatus experimentStatus,
TypeSystem typeSystem,
@@ -97,12 +100,25 @@ class ConstantEvaluationEngine {
strictInference: false,
typeProvider: typeProvider,
),
experimentStatus = experimentStatus ?? ExperimentStatus();
experimentStatus = experimentStatus ?? ExperimentStatus() {
_fromEnvironmentEvaluator = FromEnvironmentEvaluator(
typeSystem,
declaredVariables,
);
}
bool get _isNonNullableByDefault {
return (typeSystem as TypeSystemImpl).isNonNullableByDefault;
}
DartObjectImpl get _nullObject {
return DartObjectImpl(
typeSystem,
typeProvider.nullType,
NullState.NULL_STATE,
);
}
/// Check that the arguments to a call to fromEnvironment() are correct. The
/// [arguments] are the AST nodes of the arguments. The [argumentValues] are
/// the values of the unnamed arguments. The [namedArgumentValues] are the
@@ -190,8 +206,7 @@ class ConstantEvaluationEngine {
constant.evaluationResult =
EvaluationResultImpl(dartObject, errorListener.errors);
} else {
constant.evaluationResult =
EvaluationResultImpl(typeProvider.nullObject);
constant.evaluationResult = EvaluationResultImpl(_nullObject);
}
}
} else if (constant is VariableElementImpl) {
@@ -460,7 +475,10 @@ class ConstantEvaluationEngine {
// in this case, as well as other cases involving constant expression
// circularities (e.g. "compile-time constant expression depends on
// itself")
return DartObjectImpl.validWithUnknownValue(constructor.returnType);
return DartObjectImpl.validWithUnknownValue(
typeSystem,
constructor.returnType,
);
}
int argumentCount = arguments.length;
@@ -509,27 +527,41 @@ class ConstantEvaluationEngine {
if (definingClass == typeProvider.boolType) {
DartObject valueFromEnvironment;
valueFromEnvironment =
_declaredVariables.getBool(typeProvider, variableName);
_fromEnvironmentEvaluator.getBool(variableName);
return computeValueFromEnvironment(
valueFromEnvironment,
DartObjectImpl(typeProvider.boolType, BoolState.FALSE_STATE),
namedValues);
valueFromEnvironment,
DartObjectImpl(
typeSystem,
typeProvider.boolType,
BoolState.FALSE_STATE,
),
namedValues,
);
} else if (definingClass == typeProvider.intType) {
DartObject valueFromEnvironment;
valueFromEnvironment =
_declaredVariables.getInt(typeProvider, variableName);
valueFromEnvironment = _fromEnvironmentEvaluator.getInt(variableName);
return computeValueFromEnvironment(
valueFromEnvironment,
DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE),
namedValues);
valueFromEnvironment,
DartObjectImpl(
typeSystem,
typeProvider.nullType,
NullState.NULL_STATE,
),
namedValues,
);
} else if (definingClass == typeProvider.stringType) {
DartObject valueFromEnvironment;
valueFromEnvironment =
_declaredVariables.getString(typeProvider, variableName);
_fromEnvironmentEvaluator.getString(variableName);
return computeValueFromEnvironment(
valueFromEnvironment,
DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE),
namedValues);
valueFromEnvironment,
DartObjectImpl(
typeSystem,
typeProvider.nullType,
NullState.NULL_STATE,
),
namedValues,
);
}
} else if (constructor.name == "" &&
definingClass == typeProvider.symbolType &&
@@ -540,7 +572,11 @@ class ConstantEvaluationEngine {
return null;
}
String argumentValue = argumentValues[0].toStringValue();
return DartObjectImpl(definingClass, SymbolState(argumentValue));
return DartObjectImpl(
typeSystem,
definingClass,
SymbolState(argumentValue),
);
}
// Either it's an external const factory constructor that we can't
// emulate, or an error occurred (a cycle, or a const constructor trying
@@ -548,7 +584,7 @@ class ConstantEvaluationEngine {
// In the former case, the best we can do is consider it an unknown value.
// In the latter case, the error has already been reported, so considering
// it an unknown value will suppress further errors.
return DartObjectImpl.validWithUnknownValue(definingClass);
return DartObjectImpl.validWithUnknownValue(typeSystem, definingClass);
}
ConstructorElementImpl constructorBase = constructor.declaration;
validator.beforeGetConstantInitializers(constructorBase);
@@ -560,7 +596,7 @@ class ConstantEvaluationEngine {
// const instance using a non-const constructor, or the node we're
// visiting is involved in a cycle). The error has already been reported,
// so consider it an unknown value to suppress further errors.
return DartObjectImpl.validWithUnknownValue(definingClass);
return DartObjectImpl.validWithUnknownValue(typeSystem, definingClass);
}
var fieldMap = HashMap<String, DartObjectImpl>();
@@ -639,7 +675,7 @@ class ConstantEvaluationEngine {
EvaluationResultImpl evaluationResult = baseParameter.evaluationResult;
if (evaluationResult == null) {
// No default was provided, so the default value is null.
argumentValue = typeProvider.nullObject;
argumentValue = _nullObject;
} else if (evaluationResult.value != null) {
argumentValue = evaluationResult.value;
}
@@ -775,7 +811,10 @@ class ConstantEvaluationEngine {
CompileTimeErrorCode.CONST_EVAL_THROWS_EXCEPTION, node);
}
return DartObjectImpl(
definingClass, GenericState(fieldMap, invocation: invocation));
typeSystem,
definingClass,
GenericState(fieldMap, invocation: invocation),
);
}
void evaluateSuperConstructorCall(
@@ -1105,8 +1144,13 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
}
@override
DartObjectImpl visitBooleanLiteral(BooleanLiteral node) =>
DartObjectImpl(_typeProvider.boolType, BoolState.from(node.value));
DartObjectImpl visitBooleanLiteral(BooleanLiteral node) {
return DartObjectImpl(
typeSystem,
_typeProvider.boolType,
BoolState.from(node.value),
);
}
@override
DartObjectImpl visitConditionalExpression(ConditionalExpression node) {
@@ -1164,12 +1208,19 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
ParameterizedType thenType = thenResult.type;
ParameterizedType elseType = elseResult.type;
return DartObjectImpl.validWithUnknownValue(
typeSystem.leastUpperBound(thenType, elseType) as ParameterizedType);
typeSystem,
typeSystem.leastUpperBound(thenType, elseType) as ParameterizedType,
);
}
@override
DartObjectImpl visitDoubleLiteral(DoubleLiteral node) =>
DartObjectImpl(_typeProvider.doubleType, DoubleState(node.value));
DartObjectImpl visitDoubleLiteral(DoubleLiteral node) {
return DartObjectImpl(
typeSystem,
_typeProvider.doubleType,
DoubleState(node.value),
);
}
@override
DartObjectImpl visitInstanceCreationExpression(
@@ -1194,9 +1245,16 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
DartObjectImpl visitIntegerLiteral(IntegerLiteral node) {
if (node.staticType == _typeProvider.doubleType) {
return DartObjectImpl(
_typeProvider.doubleType, DoubleState(node.value?.toDouble()));
typeSystem,
_typeProvider.doubleType,
DoubleState(node.value?.toDouble()),
);
}
return DartObjectImpl(_typeProvider.intType, IntState(node.value));
return DartObjectImpl(
typeSystem,
_typeProvider.intType,
IntState(node.value),
);
}
@override
@@ -1210,8 +1268,13 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
}
@override
DartObjectImpl visitInterpolationString(InterpolationString node) =>
DartObjectImpl(_typeProvider.stringType, StringState(node.value));
DartObjectImpl visitInterpolationString(InterpolationString node) {
return DartObjectImpl(
typeSystem,
_typeProvider.stringType,
StringState(node.value),
);
}
@override
DartObjectImpl visitIsExpression(IsExpression node) {
@@ -1246,7 +1309,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
? nodeType.typeArguments[0]
: _typeProvider.dynamicType;
InterfaceType listType = _typeProvider.listType2(elementType);
return DartObjectImpl(listType, ListState(list));
return DartObjectImpl(typeSystem, listType, ListState(list));
}
@override
@@ -1286,7 +1349,9 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
}
@override
DartObjectImpl visitNullLiteral(NullLiteral node) => _typeProvider.nullObject;
DartObjectImpl visitNullLiteral(NullLiteral node) {
return evaluationEngine._nullObject;
}
@override
DartObjectImpl visitParenthesizedExpression(ParenthesizedExpression node) =>
@@ -1302,7 +1367,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
prefixElement is! ExtensionElement) {
DartObjectImpl prefixResult = prefixNode.accept(this);
if (_isStringLength(prefixResult, node.identifier)) {
return prefixResult.stringLength(_typeProvider);
return prefixResult.stringLength(typeSystem);
}
}
// importPrefix.CONST
@@ -1342,7 +1407,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
if (node.target != null) {
DartObjectImpl prefixResult = node.target.accept(this);
if (_isStringLength(prefixResult, node.propertyName)) {
return prefixResult.stringLength(_typeProvider);
return prefixResult.stringLength(typeSystem);
}
}
return _getConstantValue(node, node.propertyName.staticElement);
@@ -1382,7 +1447,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
}
}
InterfaceType mapType = _typeProvider.mapType2(keyType, valueType);
return DartObjectImpl(mapType, MapState(map));
return DartObjectImpl(typeSystem, mapType, MapState(map));
} else {
if (!node.isConst) {
_errorReporter.reportErrorForNode(
@@ -1403,7 +1468,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
? nodeType.typeArguments[0]
: _typeProvider.dynamicType;
InterfaceType setType = _typeProvider.setType2(elementType);
return DartObjectImpl(setType, SetState(set));
return DartObjectImpl(typeSystem, setType, SetState(set));
}
}
@@ -1417,8 +1482,13 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
}
@override
DartObjectImpl visitSimpleStringLiteral(SimpleStringLiteral node) =>
DartObjectImpl(_typeProvider.stringType, StringState(node.value));
DartObjectImpl visitSimpleStringLiteral(SimpleStringLiteral node) {
return DartObjectImpl(
typeSystem,
_typeProvider.stringType,
StringState(node.value),
);
}
@override
DartObjectImpl visitStringInterpolation(StringInterpolation node) {
@@ -1447,7 +1517,10 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
buffer.write(components[i].lexeme);
}
return DartObjectImpl(
_typeProvider.symbolType, SymbolState(buffer.toString()));
typeSystem,
_typeProvider.symbolType,
SymbolState(buffer.toString()),
);
}
@override
@@ -1456,7 +1529,11 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
if (_hasTypeParameterReference(type)) {
return super.visitTypeName(node);
}
return DartObjectImpl(_typeProvider.typeType, TypeState(type));
return DartObjectImpl(
typeSystem,
_typeProvider.typeType,
TypeState(type),
);
}
/// Add the entries produced by evaluating the given collection [element] to
@@ -1626,7 +1703,11 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
ExecutableElement function = element;
if (function.isStatic) {
var functionType = node.staticType;
return DartObjectImpl(functionType, FunctionState(function));
return DartObjectImpl(
typeSystem,
functionType,
FunctionState(function),
);
}
} else if (variableElement is ClassElement) {
var type = variableElement.instantiate(
@@ -1635,9 +1716,14 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
.toList(),
nullabilitySuffix: NullabilitySuffix.star,
);
return DartObjectImpl(_typeProvider.typeType, TypeState(type));
return DartObjectImpl(
typeSystem,
_typeProvider.typeType,
TypeState(type),
);
} else if (variableElement is DynamicElementImpl) {
return DartObjectImpl(
typeSystem,
_typeProvider.typeType,
TypeState(_typeProvider.dynamicType),
);
@@ -1648,9 +1734,14 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
.toList(),
nullabilitySuffix: NullabilitySuffix.star,
);
return DartObjectImpl(_typeProvider.typeType, TypeState(type));
return DartObjectImpl(
typeSystem,
_typeProvider.typeType,
TypeState(type),
);
} else if (variableElement is NeverElementImpl) {
return DartObjectImpl(
typeSystem,
_typeProvider.typeType,
TypeState(_typeProvider.neverType),
);
@@ -1693,7 +1784,7 @@ class ConstantVisitor extends UnifyingAstVisitor<DartObjectImpl> {
if (expressionValue != null) {
return expressionValue;
}
return _typeProvider.nullObject;
return evaluationEngine._nullObject;
}
/// Return `true` if the [type] has a type parameter reference, so is not
@@ -1720,10 +1811,6 @@ class DartObjectComputer {
DartObjectComputer(this._errorReporter, this._evaluationEngine);
/// Convenience getter to gain access to the [evaluationEngine]'s type
/// provider.
TypeProvider get _typeProvider => _evaluationEngine.typeProvider;
/// Convenience getter to gain access to the [evaluationEngine]'s type system.
TypeSystem get _typeSystem => _evaluationEngine.typeSystem;
@@ -1731,7 +1818,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.add(_typeProvider, rightOperand);
return leftOperand.add(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
return null;
@@ -1747,7 +1834,7 @@ class DartObjectComputer {
AstNode node, DartObjectImpl evaluationResult) {
if (evaluationResult != null) {
try {
return evaluationResult.convertToBool(_typeProvider);
return evaluationResult.convertToBool(_typeSystem);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1758,7 +1845,7 @@ class DartObjectComputer {
DartObjectImpl bitNot(Expression node, DartObjectImpl evaluationResult) {
if (evaluationResult != null) {
try {
return evaluationResult.bitNot(_typeProvider);
return evaluationResult.bitNot(_typeSystem);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1770,7 +1857,7 @@ class DartObjectComputer {
AsExpression node, DartObjectImpl expression, DartObjectImpl type) {
if (expression != null && type != null) {
try {
return expression.castToType(_typeProvider, _typeSystem, type);
return expression.castToType(_typeSystem, type);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1782,7 +1869,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.concatenate(_typeProvider, rightOperand);
return leftOperand.concatenate(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1794,7 +1881,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.divide(_typeProvider, rightOperand);
return leftOperand.divide(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1806,7 +1893,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand, bool allowBool) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.eagerAnd(_typeProvider, rightOperand, allowBool);
return leftOperand.eagerAnd(_typeSystem, rightOperand, allowBool);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1818,7 +1905,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand, bool allowBool) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.eagerOr(_typeProvider, rightOperand, allowBool);
return leftOperand.eagerOr(_typeSystem, rightOperand, allowBool);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1841,7 +1928,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand, bool allowBool) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.eagerXor(_typeProvider, rightOperand, allowBool);
return leftOperand.eagerXor(_typeSystem, rightOperand, allowBool);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1853,7 +1940,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.equalEqual(_typeProvider, rightOperand);
return leftOperand.equalEqual(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1865,7 +1952,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.greaterThan(_typeProvider, rightOperand);
return leftOperand.greaterThan(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1877,7 +1964,7 @@ class DartObjectComputer {
DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.greaterThanOrEqual(_typeProvider, rightOperand);
return leftOperand.greaterThanOrEqual(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1889,7 +1976,7 @@ class DartObjectComputer {
DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.integerDivide(_typeProvider, rightOperand);
return leftOperand.integerDivide(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1901,7 +1988,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.isIdentical(_typeProvider, rightOperand);
return leftOperand.isIdentical2(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1913,7 +2000,7 @@ class DartObjectComputer {
DartObjectImpl Function() rightOperandComputer) {
if (leftOperand != null) {
try {
return leftOperand.lazyAnd(_typeProvider, rightOperandComputer);
return leftOperand.lazyAnd(_typeSystem, rightOperandComputer);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1925,7 +2012,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.lazyEqualEqual(_typeProvider, rightOperand);
return leftOperand.lazyEqualEqual(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1937,7 +2024,7 @@ class DartObjectComputer {
DartObjectImpl Function() rightOperandComputer) {
if (leftOperand != null) {
try {
return leftOperand.lazyOr(_typeProvider, rightOperandComputer);
return leftOperand.lazyOr(_typeSystem, rightOperandComputer);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1962,7 +2049,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.lessThan(_typeProvider, rightOperand);
return leftOperand.lessThan(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1974,7 +2061,7 @@ class DartObjectComputer {
DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.lessThanOrEqual(_typeProvider, rightOperand);
return leftOperand.lessThanOrEqual(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1985,7 +2072,7 @@ class DartObjectComputer {
DartObjectImpl logicalNot(Expression node, DartObjectImpl evaluationResult) {
if (evaluationResult != null) {
try {
return evaluationResult.logicalNot(_typeProvider);
return evaluationResult.logicalNot(_typeSystem);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -1997,7 +2084,7 @@ class DartObjectComputer {
DartObjectImpl leftOperand, DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.logicalShiftRight(_typeProvider, rightOperand);
return leftOperand.logicalShiftRight(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2009,7 +2096,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.minus(_typeProvider, rightOperand);
return leftOperand.minus(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2020,7 +2107,7 @@ class DartObjectComputer {
DartObjectImpl negated(Expression node, DartObjectImpl evaluationResult) {
if (evaluationResult != null) {
try {
return evaluationResult.negated(_typeProvider);
return evaluationResult.negated(_typeSystem);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2032,7 +2119,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.notEqual(_typeProvider, rightOperand);
return leftOperand.notEqual(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2044,7 +2131,7 @@ class DartObjectComputer {
AstNode node, DartObjectImpl evaluationResult) {
if (evaluationResult != null) {
try {
return evaluationResult.performToString(_typeProvider);
return evaluationResult.performToString(_typeSystem);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2056,7 +2143,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.remainder(_typeProvider, rightOperand);
return leftOperand.remainder(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2068,7 +2155,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.shiftLeft(_typeProvider, rightOperand);
return leftOperand.shiftLeft(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2080,7 +2167,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.shiftRight(_typeProvider, rightOperand);
return leftOperand.shiftRight(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2096,7 +2183,7 @@ class DartObjectComputer {
if (evaluationResult.value != null) {
try {
return EvaluationResultImpl(
evaluationResult.value.stringLength(_typeProvider));
evaluationResult.value.stringLength(_typeSystem));
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2108,7 +2195,7 @@ class DartObjectComputer {
DartObjectImpl rightOperand) {
if (leftOperand != null && rightOperand != null) {
try {
return leftOperand.times(_typeProvider, rightOperand);
return leftOperand.times(_typeSystem, rightOperand);
} on EvaluationException catch (exception) {
_errorReporter.reportErrorForNode(exception.errorCode, node);
}
@@ -2120,10 +2207,9 @@ class DartObjectComputer {
IsExpression node, DartObjectImpl expression, DartObjectImpl type) {
if (expression != null && type != null) {
try {
DartObjectImpl result =
expression.hasType(_typeProvider, _typeSystem, type);
DartObjectImpl result = expression.hasType(_typeSystem, type);
if (node.notOperator != null) {
return result.logicalNot(_typeProvider);
return result.logicalNot(_typeSystem);
}
return result;
} on EvaluationException catch (exception) {
@@ -0,0 +1,99 @@
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/analysis/declared_variables.dart';
import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/src/dart/constant/value.dart';
import 'package:analyzer/src/generated/type_system.dart';
class FromEnvironmentEvaluator {
final TypeSystemImpl _typeSystem;
final DeclaredVariables _declaredVariables;
FromEnvironmentEvaluator(this._typeSystem, this._declaredVariables);
/// Return the value of the variable with the given [name] interpreted as a
/// 'boolean' value. If the variable is not defined (or [name] is `null`), a
/// DartObject representing "unknown" is returned. If the value cannot be
/// parsed as a boolean, a DartObject representing 'null' is returned.
DartObject getBool(String name) {
String value = _declaredVariables.get(name);
if (value == null) {
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.boolType,
BoolState.UNKNOWN_VALUE,
);
}
if (value == "true") {
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.boolType,
BoolState.TRUE_STATE,
);
} else if (value == "false") {
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.boolType,
BoolState.FALSE_STATE,
);
}
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.nullType,
NullState.NULL_STATE,
);
}
/// Return the value of the variable with the given [name] interpreted as an
/// integer value. If the variable is not defined (or [name] is `null`), a
/// DartObject representing "unknown" is returned. If the value cannot be
/// parsed as an integer, a DartObject representing 'null' is returned.
DartObject getInt(String name) {
String value = _declaredVariables.get(name);
if (value == null) {
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.intType,
IntState.UNKNOWN_VALUE,
);
}
int bigInteger;
try {
bigInteger = int.parse(value);
} on FormatException {
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.nullType,
NullState.NULL_STATE,
);
}
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.intType,
IntState(bigInteger),
);
}
/// Return the value of the variable with the given [name] interpreted as a
/// String value, or `null` if the variable is not defined. Return the value
/// of the variable with the given name interpreted as a String value. If the
/// variable is not defined (or [name] is `null`), a DartObject representing
/// "unknown" is returned.
DartObject getString(String name) {
String value = _declaredVariables.get(name);
if (value == null) {
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.stringType,
StringState.UNKNOWN_VALUE,
);
}
return DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.stringType,
StringState(value),
);
}
}
+293 -161
View File
@@ -9,9 +9,9 @@ import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/dart/element/type_system.dart';
import 'package:analyzer/error/error.dart';
import 'package:analyzer/src/error/codes.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:analyzer/src/generated/utilities_general.dart';
/// The state of an object representing a boolean value.
@@ -146,6 +146,9 @@ class ConstructorInvocation {
/// A representation of an instance of a Dart class.
class DartObjectImpl implements DartObject {
// ignore: unused_field
final TypeSystemImpl _typeSystem;
@override
final ParameterizedType type;
@@ -153,22 +156,25 @@ class DartObjectImpl implements DartObject {
final InstanceState _state;
/// Initialize a newly created object to have the given [type] and [_state].
DartObjectImpl(this.type, this._state);
DartObjectImpl(this._typeSystem, this.type, this._state);
/// Create an object to represent an unknown value.
factory DartObjectImpl.validWithUnknownValue(ParameterizedType type) {
factory DartObjectImpl.validWithUnknownValue(
TypeSystemImpl typeSystem,
ParameterizedType type,
) {
if (type.element.library.isDartCore) {
if (type.isDartCoreBool) {
return DartObjectImpl(type, BoolState.UNKNOWN_VALUE);
return DartObjectImpl(typeSystem, type, BoolState.UNKNOWN_VALUE);
} else if (type.isDartCoreDouble) {
return DartObjectImpl(type, DoubleState.UNKNOWN_VALUE);
return DartObjectImpl(typeSystem, type, DoubleState.UNKNOWN_VALUE);
} else if (type.isDartCoreInt) {
return DartObjectImpl(type, IntState.UNKNOWN_VALUE);
return DartObjectImpl(typeSystem, type, IntState.UNKNOWN_VALUE);
} else if (type.isDartCoreString) {
return DartObjectImpl(type, StringState.UNKNOWN_VALUE);
return DartObjectImpl(typeSystem, type, StringState.UNKNOWN_VALUE);
}
}
return DartObjectImpl(type, GenericState.UNKNOWN_VALUE);
return DartObjectImpl(typeSystem, type, GenericState.UNKNOWN_VALUE);
}
Map<String, DartObjectImpl> get fields => _state.fields;
@@ -208,35 +214,50 @@ class DartObjectImpl implements DartObject {
}
/// Return the result of invoking the '+' operator on this object with the
/// given [rightOperand]. The [typeProvider] is the type provider used to find
/// known types.
/// given [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl add(TypeProvider typeProvider, DartObjectImpl rightOperand) {
DartObjectImpl add(TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
InstanceState result = _state.add(rightOperand._state);
if (result is IntState) {
return DartObjectImpl(typeProvider.intType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
result,
);
} else if (result is DoubleState) {
return DartObjectImpl(typeProvider.doubleType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.doubleType,
result,
);
} else if (result is StringState) {
return DartObjectImpl(typeProvider.stringType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.stringType,
result,
);
}
// We should never get here.
throw StateError("add returned a ${result.runtimeType}");
}
/// Return the result of invoking the '~' operator on this object. The
/// [typeProvider] is the type provider used to find known types.
/// Return the result of invoking the '~' operator on this object.
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl bitNot(TypeProvider typeProvider) =>
DartObjectImpl(typeProvider.intType, _state.bitNot());
DartObjectImpl bitNot(TypeSystemImpl typeSystem) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
_state.bitNot(),
);
}
/// Return the result of casting this object to the given [castType].
DartObjectImpl castToType(TypeProvider typeProvider, TypeSystem typeSystem,
DartObjectImpl castType) {
DartObjectImpl castToType(
TypeSystemImpl typeSystem, DartObjectImpl castType) {
_assertType(castType);
if (isNull) {
return this;
@@ -249,119 +270,147 @@ class DartObjectImpl implements DartObject {
}
/// Return the result of invoking the ' ' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl concatenate(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.stringType, _state.concatenate(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.stringType,
_state.concatenate(rightOperand._state),
);
}
/// Return the result of applying boolean conversion to this object. The
/// [typeProvider] is the type provider used to find known types.
/// Return the result of applying boolean conversion to this object.
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl convertToBool(TypeProvider typeProvider) {
InterfaceType boolType = typeProvider.boolType;
DartObjectImpl convertToBool(TypeSystemImpl typeSystem) {
InterfaceType boolType = typeSystem.typeProvider.boolType;
if (identical(type, boolType)) {
return this;
}
return DartObjectImpl(boolType, _state.convertToBool());
return DartObjectImpl(typeSystem, boolType, _state.convertToBool());
}
/// Return the result of invoking the '/' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for
/// an object of this kind.
DartObjectImpl divide(
TypeProvider typeProvider, DartObjectImpl rightOperand) {
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
InstanceState result = _state.divide(rightOperand._state);
if (result is IntState) {
return DartObjectImpl(typeProvider.intType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
result,
);
} else if (result is DoubleState) {
return DartObjectImpl(typeProvider.doubleType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.doubleType,
result,
);
}
// We should never get here.
throw StateError("divide returned a ${result.runtimeType}");
}
/// Return the result of invoking the '&' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl eagerAnd(
TypeProvider typeProvider, DartObjectImpl rightOperand, bool allowBool) {
TypeSystemImpl typeSystem, DartObjectImpl rightOperand, bool allowBool) {
if (allowBool && isBool && rightOperand.isBool) {
return DartObjectImpl(
typeProvider.boolType, _state.logicalAnd(rightOperand._state));
typeSystem,
typeSystem.typeProvider.boolType,
_state.logicalAnd(rightOperand._state),
);
} else if (isInt && rightOperand.isInt) {
return DartObjectImpl(
typeProvider.intType, _state.bitAnd(rightOperand._state));
typeSystem,
typeSystem.typeProvider.intType,
_state.bitAnd(rightOperand._state),
);
}
throw EvaluationException(CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_INT);
}
/// Return the result of invoking the '|' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl eagerOr(
TypeProvider typeProvider, DartObjectImpl rightOperand, bool allowBool) {
TypeSystemImpl typeSystem, DartObjectImpl rightOperand, bool allowBool) {
if (allowBool && isBool && rightOperand.isBool) {
return DartObjectImpl(
typeProvider.boolType, _state.logicalOr(rightOperand._state));
typeSystem,
typeSystem.typeProvider.boolType,
_state.logicalOr(rightOperand._state),
);
} else if (isInt && rightOperand.isInt) {
return DartObjectImpl(
typeProvider.intType, _state.bitOr(rightOperand._state));
typeSystem,
typeSystem.typeProvider.intType,
_state.bitOr(rightOperand._state),
);
}
throw EvaluationException(CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_INT);
}
/// Return the result of invoking the '^' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl eagerXor(
TypeProvider typeProvider, DartObjectImpl rightOperand, bool allowBool) {
TypeSystemImpl typeSystem, DartObjectImpl rightOperand, bool allowBool) {
if (allowBool && isBool && rightOperand.isBool) {
return DartObjectImpl(
typeProvider.boolType, _state.logicalXor(rightOperand._state));
typeSystem,
typeSystem.typeProvider.boolType,
_state.logicalXor(rightOperand._state),
);
} else if (isInt && rightOperand.isInt) {
return DartObjectImpl(
typeProvider.intType, _state.bitXor(rightOperand._state));
typeSystem,
typeSystem.typeProvider.intType,
_state.bitXor(rightOperand._state),
);
}
throw EvaluationException(CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_INT);
}
/// Return the result of invoking the '==' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl equalEqual(
TypeProvider typeProvider, DartObjectImpl rightOperand) {
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
if (isNull || rightOperand.isNull) {
return DartObjectImpl(
typeProvider.boolType,
isNull && rightOperand.isNull
? BoolState.TRUE_STATE
: BoolState.FALSE_STATE);
typeSystem,
typeSystem.typeProvider.boolType,
isNull && rightOperand.isNull
? BoolState.TRUE_STATE
: BoolState.FALSE_STATE,
);
}
if (isBoolNumStringOrNull) {
return DartObjectImpl(
typeProvider.boolType, _state.equalEqual(rightOperand._state));
typeSystem,
typeSystem.typeProvider.boolType,
_state.equalEqual(rightOperand._state),
);
}
throw EvaluationException(
CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING);
@@ -387,38 +436,43 @@ class DartObjectImpl implements DartObject {
}
/// Return the result of invoking the '&gt;' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl greaterThan(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.boolType, _state.greaterThan(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.boolType,
_state.greaterThan(rightOperand._state),
);
}
/// Return the result of invoking the '&gt;=' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl greaterThanOrEqual(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(typeProvider.boolType,
_state.greaterThanOrEqual(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.boolType,
_state.greaterThanOrEqual(rightOperand._state),
);
}
/// Return the result of testing whether this object has the given
/// [testedType].
DartObjectImpl hasType(TypeProvider typeProvider, TypeSystem typeSystem,
DartObjectImpl testedType) {
DartObjectImpl hasType(TypeSystemImpl typeSystem, DartObjectImpl testedType) {
_assertType(testedType);
DartType typeType = (testedType._state as TypeState)._type;
BoolState state;
if (isNull) {
if (typeType == typeProvider.objectType ||
typeType == typeProvider.dynamicType ||
typeType == typeProvider.nullType) {
if (typeType == typeSystem.typeProvider.objectType ||
typeType == typeSystem.typeProvider.dynamicType ||
typeType == typeSystem.typeProvider.nullType) {
state = BoolState.TRUE_STATE;
} else {
state = BoolState.FALSE_STATE;
@@ -426,232 +480,310 @@ class DartObjectImpl implements DartObject {
} else {
state = BoolState.from(typeSystem.isSubtypeOf(type, typeType));
}
return DartObjectImpl(typeProvider.boolType, state);
return DartObjectImpl(typeSystem, typeSystem.typeProvider.boolType, state);
}
/// Return the result of invoking the '~/' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl integerDivide(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.intType, _state.integerDivide(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
_state.integerDivide(rightOperand._state),
);
}
/// Return the result of invoking the identical function on this object with
/// the [rightOperand]. The [typeProvider] is the type provider used to find
/// known types.
@Deprecated('Use isIdentical2() instead')
DartObjectImpl isIdentical(
TypeProvider typeProvider, DartObjectImpl rightOperand) {
var typeSystem = TypeSystemImpl(
implicitCasts: false,
isNonNullableByDefault: false,
strictInference: false,
typeProvider: typeProvider,
);
return isIdentical2(typeSystem, rightOperand);
}
/// Return the result of invoking the identical function on this object with
/// the [rightOperand].
DartObjectImpl isIdentical2(
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeProvider.boolType, _state.isIdentical(rightOperand._state));
typeSystem,
typeSystem.typeProvider.boolType,
_state.isIdentical(rightOperand._state),
);
}
/// Return the result of invoking the '&&' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl lazyAnd(TypeProvider typeProvider,
DartObjectImpl Function() rightOperandComputer) =>
DartObjectImpl(typeProvider.boolType,
_state.lazyAnd(() => rightOperandComputer()?._state));
DartObjectImpl lazyAnd(TypeSystemImpl typeSystem,
DartObjectImpl Function() rightOperandComputer) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.boolType,
_state.lazyAnd(() => rightOperandComputer()?._state),
);
}
/// Return the result of invoking the '==' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl lazyEqualEqual(
TypeProvider typeProvider, DartObjectImpl rightOperand) {
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
if (isNull || rightOperand.isNull) {
return DartObjectImpl(
typeProvider.boolType,
isNull && rightOperand.isNull
? BoolState.TRUE_STATE
: BoolState.FALSE_STATE);
typeSystem,
typeSystem.typeProvider.boolType,
isNull && rightOperand.isNull
? BoolState.TRUE_STATE
: BoolState.FALSE_STATE,
);
}
if (isBoolNumStringOrNull) {
return DartObjectImpl(
typeProvider.boolType, _state.lazyEqualEqual(rightOperand._state));
typeSystem,
typeSystem.typeProvider.boolType,
_state.lazyEqualEqual(rightOperand._state),
);
}
throw EvaluationException(
CompileTimeErrorCode.CONST_EVAL_TYPE_BOOL_NUM_STRING);
}
/// Return the result of invoking the '||' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl lazyOr(TypeProvider typeProvider,
DartObjectImpl lazyOr(TypeSystemImpl typeSystem,
DartObjectImpl Function() rightOperandComputer) =>
DartObjectImpl(typeProvider.boolType,
_state.lazyOr(() => rightOperandComputer()?._state));
DartObjectImpl(
typeSystem,
typeSystem.typeProvider.boolType,
_state.lazyOr(() => rightOperandComputer()?._state),
);
/// Return the result of invoking the '&lt;' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl lessThan(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.boolType, _state.lessThan(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.boolType,
_state.lessThan(rightOperand._state),
);
}
/// Return the result of invoking the '&lt;=' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl lessThanOrEqual(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.boolType, _state.lessThanOrEqual(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.boolType,
_state.lessThanOrEqual(rightOperand._state),
);
}
/// Return the result of invoking the '!' operator on this object. The
/// [typeProvider] is the type provider used to find known types.
/// Return the result of invoking the '!' operator on this object.
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl logicalNot(TypeProvider typeProvider) =>
DartObjectImpl(typeProvider.boolType, _state.logicalNot());
DartObjectImpl logicalNot(TypeSystemImpl typeSystem) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.boolType,
_state.logicalNot(),
);
}
/// Return the result of invoking the '&gt;&gt;&gt;' operator on this object
/// with the [rightOperand]. The [typeProvider] is the type provider used to
/// find known types.
/// with the [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl logicalShiftRight(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.intType, _state.logicalShiftRight(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
_state.logicalShiftRight(rightOperand._state),
);
}
/// Return the result of invoking the '-' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl minus(TypeProvider typeProvider, DartObjectImpl rightOperand) {
DartObjectImpl minus(TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
InstanceState result = _state.minus(rightOperand._state);
if (result is IntState) {
return DartObjectImpl(typeProvider.intType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
result,
);
} else if (result is DoubleState) {
return DartObjectImpl(typeProvider.doubleType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.doubleType,
result,
);
}
// We should never get here.
throw StateError("minus returned a ${result.runtimeType}");
}
/// Return the result of invoking the '-' operator on this object. The
/// [typeProvider] is the type provider used to find known types.
/// Return the result of invoking the '-' operator on this object.
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl negated(TypeProvider typeProvider) {
DartObjectImpl negated(TypeSystemImpl typeSystem) {
InstanceState result = _state.negated();
if (result is IntState) {
return DartObjectImpl(typeProvider.intType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
result,
);
} else if (result is DoubleState) {
return DartObjectImpl(typeProvider.doubleType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.doubleType,
result,
);
}
// We should never get here.
throw StateError("negated returned a ${result.runtimeType}");
}
/// Return the result of invoking the '!=' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl notEqual(
TypeProvider typeProvider, DartObjectImpl rightOperand) {
return equalEqual(typeProvider, rightOperand).logicalNot(typeProvider);
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return equalEqual(typeSystem, rightOperand).logicalNot(typeSystem);
}
/// Return the result of converting this object to a 'String'. The
/// [typeProvider] is the type provider used to find known types.
/// Return the result of converting this object to a 'String'.
///
/// Throws an [EvaluationException] if the object cannot be converted to a
/// 'String'.
DartObjectImpl performToString(TypeProvider typeProvider) {
InterfaceType stringType = typeProvider.stringType;
DartObjectImpl performToString(TypeSystemImpl typeSystem) {
InterfaceType stringType = typeSystem.typeProvider.stringType;
if (identical(type, stringType)) {
return this;
}
return DartObjectImpl(stringType, _state.convertToString());
return DartObjectImpl(typeSystem, stringType, _state.convertToString());
}
/// Return the result of invoking the '%' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl remainder(
TypeProvider typeProvider, DartObjectImpl rightOperand) {
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
InstanceState result = _state.remainder(rightOperand._state);
if (result is IntState) {
return DartObjectImpl(typeProvider.intType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
result,
);
} else if (result is DoubleState) {
return DartObjectImpl(typeProvider.doubleType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.doubleType,
result,
);
}
// We should never get here.
throw StateError("remainder returned a ${result.runtimeType}");
}
/// Return the result of invoking the '&lt;&lt;' operator on this object with
/// the [rightOperand]. The [typeProvider] is the type provider used to find
/// known types.
/// the [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl shiftLeft(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.intType, _state.shiftLeft(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
_state.shiftLeft(rightOperand._state),
);
}
/// Return the result of invoking the '&gt;&gt;' operator on this object with
/// the [rightOperand]. The [typeProvider] is the type provider used to find
/// known types.
/// the [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl shiftRight(
TypeProvider typeProvider, DartObjectImpl rightOperand) =>
DartObjectImpl(
typeProvider.intType, _state.shiftRight(rightOperand._state));
TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
_state.shiftRight(rightOperand._state),
);
}
/// Return the result of invoking the 'length' getter on this object. The
/// [typeProvider] is the type provider used to find known types.
/// Return the result of invoking the 'length' getter on this object.
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl stringLength(TypeProvider typeProvider) =>
DartObjectImpl(typeProvider.intType, _state.stringLength());
DartObjectImpl stringLength(TypeSystemImpl typeSystem) {
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
_state.stringLength(),
);
}
/// Return the result of invoking the '*' operator on this object with the
/// [rightOperand]. The [typeProvider] is the type provider used to find known
/// types.
/// [rightOperand].
///
/// Throws an [EvaluationException] if the operator is not appropriate for an
/// object of this kind.
DartObjectImpl times(TypeProvider typeProvider, DartObjectImpl rightOperand) {
DartObjectImpl times(TypeSystemImpl typeSystem, DartObjectImpl rightOperand) {
InstanceState result = _state.times(rightOperand._state);
if (result is IntState) {
return DartObjectImpl(typeProvider.intType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.intType,
result,
);
} else if (result is DoubleState) {
return DartObjectImpl(typeProvider.doubleType, result);
return DartObjectImpl(
typeSystem,
typeSystem.typeProvider.doubleType,
result,
);
}
// We should never get here.
throw StateError("times returned a ${result.runtimeType}");
+17 -4
View File
@@ -1824,9 +1824,17 @@ class ConstFieldElementImpl_EnumValue extends ConstFieldElementImpl_ofEnum {
EvaluationResultImpl get evaluationResult {
if (_evaluationResult == null) {
Map<String, DartObjectImpl> fieldMap = <String, DartObjectImpl>{
name: DartObjectImpl(library.typeProvider.intType, IntState(_index))
name: DartObjectImpl(
library.typeSystem,
library.typeProvider.intType,
IntState(_index),
)
};
DartObjectImpl value = DartObjectImpl(type, GenericState(fieldMap));
DartObjectImpl value = DartObjectImpl(
library.typeSystem,
type,
GenericState(fieldMap),
);
_evaluationResult = EvaluationResultImpl(value);
}
return _evaluationResult;
@@ -1868,8 +1876,13 @@ class ConstFieldElementImpl_EnumValues extends ConstFieldElementImpl_ofEnum {
constantValues.add(field.evaluationResult.value);
}
}
_evaluationResult =
EvaluationResultImpl(DartObjectImpl(type, ListState(constantValues)));
_evaluationResult = EvaluationResultImpl(
DartObjectImpl(
library.typeSystem,
type,
ListState(constantValues),
),
);
}
return _evaluationResult;
}
@@ -8,6 +8,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/src/dart/constant/value.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:meta/meta.dart';
/// Provide common functionality shared by the various TypeProvider
@@ -363,10 +364,20 @@ class TypeProviderImpl extends TypeProviderBase {
return _nullElement ??= _getClassElement(_coreLibrary, 'Null');
}
@deprecated
@override
DartObjectImpl get nullObject {
if (_nullObject == null) {
_nullObject = DartObjectImpl(nullType, NullState.NULL_STATE);
_nullObject = DartObjectImpl(
TypeSystemImpl(
implicitCasts: false,
isNonNullableByDefault: false,
strictInference: false,
typeProvider: this,
),
nullType,
NullState.NULL_STATE,
);
}
return _nullObject;
}
@@ -448,12 +448,13 @@ class DeadCodeVerifier extends RecursiveAstVisitor<void> {
/// is not a constant boolean value.
EvaluationResultImpl _getConstantBooleanValue(Expression expression) {
if (expression is BooleanLiteral) {
if (expression.value) {
return EvaluationResultImpl(DartObjectImpl(null, BoolState.from(true)));
} else {
return EvaluationResultImpl(
DartObjectImpl(null, BoolState.from(false)));
}
return EvaluationResultImpl(
DartObjectImpl(
_typeSystem,
_typeSystem.typeProvider.boolType,
BoolState.from(expression.value),
),
);
}
// Don't consider situations where we could evaluate to a constant boolean
@@ -18,6 +18,7 @@ import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/testing/ast_test_factory.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:analyzer/src/generated/utilities_dart.dart';
import 'package:meta/meta.dart';
import 'package:path/path.dart';
@@ -153,8 +154,15 @@ class ElementFactory {
[List<DartType> argumentTypes]) =>
constructorElement(definingClass, name, false, argumentTypes);
@deprecated
static EnumElementImpl enumElement(TypeProvider typeProvider, String enumName,
[List<String> constantNames]) {
var typeSystem = TypeSystemImpl(
implicitCasts: false,
isNonNullableByDefault: false,
strictInference: false,
typeProvider: typeProvider,
);
//
// Build the enum.
//
@@ -198,10 +206,12 @@ class ElementFactory {
constantElement.type = enumType;
Map<String, DartObjectImpl> fieldMap =
HashMap<String, DartObjectImpl>();
fieldMap[indexFieldName] = DartObjectImpl(intType, IntState(i));
fieldMap[indexFieldName] =
DartObjectImpl(typeSystem, intType, IntState(i));
fieldMap[nameFieldName] =
DartObjectImpl(stringType, StringState(constantName));
DartObjectImpl value = DartObjectImpl(enumType, GenericState(fieldMap));
DartObjectImpl(typeSystem, stringType, StringState(constantName));
DartObjectImpl value =
DartObjectImpl(typeSystem, enumType, GenericState(fieldMap));
constantElement.evaluationResult = EvaluationResultImpl(value);
fields.add(constantElement);
}
-1
View File
@@ -441,7 +441,6 @@ class LinterContextImpl implements LinterContext {
ConstantVerifier(
errorReporter,
libraryElement,
typeProvider,
declaredVariables,
featureSet: currentUnit.unit.featureSet,
),
@@ -1,111 +0,0 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/analysis/declared_variables.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/testing/test_type_provider.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(DeclaredVariablesTest);
});
}
@reflectiveTest
class DeclaredVariablesTest {
void test_getBool_false() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables =
DeclaredVariables.fromMap({variableName: 'false'});
DartObject object = variables.getBool(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toBoolValue(), false);
}
void test_getBool_invalid() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables =
DeclaredVariables.fromMap({variableName: 'not true'});
_assertNullDartObject(
typeProvider, variables.getBool(typeProvider, variableName));
}
void test_getBool_true() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables =
DeclaredVariables.fromMap({variableName: 'true'});
DartObject object = variables.getBool(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toBoolValue(), true);
}
void test_getBool_undefined() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = DeclaredVariables();
_assertUnknownDartObject(
typeProvider.boolType, variables.getBool(typeProvider, variableName));
}
void test_getInt_invalid() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables =
DeclaredVariables.fromMap({variableName: 'four score and seven years'});
_assertNullDartObject(
typeProvider, variables.getInt(typeProvider, variableName));
}
void test_getInt_undefined() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = DeclaredVariables();
_assertUnknownDartObject(
typeProvider.intType, variables.getInt(typeProvider, variableName));
}
void test_getInt_valid() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables =
DeclaredVariables.fromMap({variableName: '23'});
DartObject object = variables.getInt(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toIntValue(), 23);
}
void test_getString_defined() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
String value = "value";
DeclaredVariables variables =
DeclaredVariables.fromMap({variableName: value});
DartObject object = variables.getString(typeProvider, variableName);
expect(object, isNotNull);
expect(object.toStringValue(), value);
}
void test_getString_undefined() {
TestTypeProvider typeProvider = TestTypeProvider();
String variableName = "var";
DeclaredVariables variables = DeclaredVariables();
_assertUnknownDartObject(typeProvider.stringType,
variables.getString(typeProvider, variableName));
}
void _assertNullDartObject(TestTypeProvider typeProvider, DartObject result) {
expect(result.type, typeProvider.nullType);
}
void _assertUnknownDartObject(DartType expectedType, DartObject result) {
expect((result as DartObjectImpl).isUnknown, isTrue);
expect(result.type, expectedType);
}
}
@@ -0,0 +1,144 @@
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/analysis/declared_variables.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/src/dart/constant/from_environment_evaluator.dart';
import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../generated/test_analysis_context.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(FromEnvironmentEvaluatorTest);
});
}
@reflectiveTest
class FromEnvironmentEvaluatorTest {
TypeProvider typeProvider;
TypeSystemImpl typeSystem;
void setUp() {
var analysisContext = TestAnalysisContext();
typeProvider = analysisContext.typeProviderLegacy;
typeSystem = analysisContext.typeSystemLegacy;
}
void test_getBool_false() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables.fromMap({variableName: 'false'}),
);
DartObject object = variables.getBool(variableName);
expect(object, isNotNull);
expect(object.toBoolValue(), false);
}
void test_getBool_invalid() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables.fromMap({variableName: 'not true'}),
);
_assertNullDartObject(
variables.getBool(variableName),
);
}
void test_getBool_true() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables.fromMap({variableName: 'true'}),
);
DartObject object = variables.getBool(variableName);
expect(object, isNotNull);
expect(object.toBoolValue(), true);
}
void test_getBool_undefined() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables(),
);
_assertUnknownDartObject(
typeProvider.boolType,
variables.getBool(variableName),
);
}
void test_getInt_invalid() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables.fromMap({variableName: 'four score and seven years'}),
);
_assertNullDartObject(
variables.getInt(variableName),
);
}
void test_getInt_undefined() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables(),
);
_assertUnknownDartObject(
typeProvider.intType,
variables.getInt(variableName),
);
}
void test_getInt_valid() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables.fromMap({variableName: '23'}),
);
DartObject object = variables.getInt(variableName);
expect(object, isNotNull);
expect(object.toIntValue(), 23);
}
void test_getString_defined() {
String variableName = "var";
String value = "value";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables.fromMap({variableName: value}),
);
DartObject object = variables.getString(variableName);
expect(object, isNotNull);
expect(object.toStringValue(), value);
}
void test_getString_undefined() {
String variableName = "var";
var variables = FromEnvironmentEvaluator(
typeSystem,
DeclaredVariables(),
);
_assertUnknownDartObject(
typeProvider.stringType,
variables.getString(variableName),
);
}
void _assertNullDartObject(DartObject result) {
expect(result.type, typeProvider.nullType);
}
void _assertUnknownDartObject(DartType expectedType, DartObject result) {
expect((result as DartObjectImpl).isUnknown, isTrue);
expect(result.type, expectedType);
}
}
@@ -4,7 +4,7 @@
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'declared_variables_test.dart' as declared_variables;
import 'from_environment_evaluator_test.dart' as declared_variables;
import 'utilities_test.dart' as utilities;
main() {
@@ -555,27 +555,6 @@ class ElementResolverTest with ResourceProviderMixin, ElementsTypesMixin {
_listener.assertNoErrors();
}
test_visitEnumDeclaration() async {
CompilationUnitElementImpl compilationUnitElement =
ElementFactory.compilationUnit('foo.dart');
EnumElementImpl enumElement =
ElementFactory.enumElement(_typeProvider, ('E'));
compilationUnitElement.enums = <ClassElement>[enumElement];
EnumDeclaration enumNode = AstTestFactory.enumDeclaration2('E', []);
Annotation annotationNode =
AstTestFactory.annotation(AstTestFactory.identifier3('a'));
annotationNode.element = ElementFactory.classElement2('A');
annotationNode.elementAnnotation =
ElementAnnotationImpl(compilationUnitElement);
enumNode.metadata.add(annotationNode);
enumNode.name.staticElement = enumElement;
List<ElementAnnotation> metadata = <ElementAnnotation>[
annotationNode.elementAnnotation
];
_resolveNode(enumNode);
expect(metadata[0].element, annotationNode.element);
}
test_visitExportDirective_noCombinators() async {
ExportDirective directive = AstTestFactory.exportDirective2(null);
directive.element = ElementFactory.exportFor(
@@ -159,7 +159,7 @@ const a = dynamic;
const a = b;
const b = 3;''');
var environment = <String, DartObjectImpl>{
'b': DartObjectImpl(typeProvider.intType, IntState(6)),
'b': DartObjectImpl(typeSystem, typeProvider.intType, IntState(6)),
};
var result = _evaluateConstant('a', lexicalEnvironment: environment);
expect(result.type, typeProvider.intType);
@@ -171,7 +171,7 @@ const b = 3;''');
const a = b;
const b = 3;''');
var environment = <String, DartObjectImpl>{
'c': DartObjectImpl(typeProvider.intType, IntState(6)),
'c': DartObjectImpl(typeSystem, typeProvider.intType, IntState(6)),
};
var result = _evaluateConstant('a', lexicalEnvironment: environment);
expect(result.type, typeProvider.intType);
@@ -5,10 +5,12 @@
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/dart/element/type_provider.dart';
import 'package:analyzer/src/generated/constant.dart';
import 'package:analyzer/src/generated/testing/test_type_provider.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../../generated/test_analysis_context.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(DartObjectImplTest);
@@ -22,7 +24,14 @@ final Matcher throwsEvaluationException =
@reflectiveTest
class DartObjectImplTest {
final TypeProvider _typeProvider = TestTypeProvider();
TypeProvider _typeProvider;
TypeSystemImpl _typeSystem;
void setUp() {
var analysisContext = TestAnalysisContext();
_typeProvider = analysisContext.typeProviderLegacy;
_typeSystem = analysisContext.typeSystemLegacy;
}
void test_add_knownDouble_knownDouble() {
_assertAdd(_doubleValue(3.0), _doubleValue(1.0), _doubleValue(2.0));
@@ -1360,8 +1369,15 @@ class DartObjectImplTest {
}
void test_shiftLeft_knownInt_tooLarge() {
_assertShiftLeft(_intValue(null), _intValue(6),
DartObjectImpl(_typeProvider.intType, IntState(LONG_MAX_VALUE)));
_assertShiftLeft(
_intValue(null),
_intValue(6),
DartObjectImpl(
_typeSystem,
_typeProvider.intType,
IntState(LONG_MAX_VALUE),
),
);
}
void test_shiftLeft_knownInt_unknownInt() {
@@ -1393,8 +1409,15 @@ class DartObjectImplTest {
}
void test_shiftRight_knownInt_tooLarge() {
_assertShiftRight(_intValue(null), _intValue(48),
DartObjectImpl(_typeProvider.intType, IntState(LONG_MAX_VALUE)));
_assertShiftRight(
_intValue(null),
_intValue(48),
DartObjectImpl(
_typeSystem,
_typeProvider.intType,
IntState(LONG_MAX_VALUE),
),
);
}
void test_shiftRight_knownInt_unknownInt() {
@@ -1488,10 +1511,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.add(_typeProvider, right);
left.add(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.add(_typeProvider, right);
DartObjectImpl result = left.add(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1504,10 +1527,10 @@ class DartObjectImplTest {
void _assertBitNot(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
expect(() {
operand.bitNot(_typeProvider);
operand.bitNot(_typeSystem);
}, throwsEvaluationException);
} else {
DartObjectImpl result = operand.bitNot(_typeProvider);
DartObjectImpl result = operand.bitNot(_typeSystem);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1522,10 +1545,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.concatenate(_typeProvider, right);
left.concatenate(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.concatenate(_typeProvider, right);
DartObjectImpl result = left.concatenate(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1540,10 +1563,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.divide(_typeProvider, right);
left.divide(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.divide(_typeProvider, right);
DartObjectImpl result = left.divide(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1558,10 +1581,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.eagerAnd(_typeProvider, right, false);
left.eagerAnd(_typeSystem, right, false);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.eagerAnd(_typeProvider, right, false);
DartObjectImpl result = left.eagerAnd(_typeSystem, right, false);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1576,10 +1599,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.eagerOr(_typeProvider, right, false);
left.eagerOr(_typeSystem, right, false);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.eagerOr(_typeProvider, right, false);
DartObjectImpl result = left.eagerOr(_typeSystem, right, false);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1594,10 +1617,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.eagerXor(_typeProvider, right, false);
left.eagerXor(_typeSystem, right, false);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.eagerXor(_typeProvider, right, false);
DartObjectImpl result = left.eagerXor(_typeSystem, right, false);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1612,10 +1635,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.equalEqual(_typeProvider, right);
left.equalEqual(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.equalEqual(_typeProvider, right);
DartObjectImpl result = left.equalEqual(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1630,10 +1653,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.greaterThan(_typeProvider, right);
left.greaterThan(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.greaterThan(_typeProvider, right);
DartObjectImpl result = left.greaterThan(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1648,10 +1671,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.greaterThanOrEqual(_typeProvider, right);
left.greaterThanOrEqual(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.greaterThanOrEqual(_typeProvider, right);
DartObjectImpl result = left.greaterThanOrEqual(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1663,7 +1686,7 @@ class DartObjectImplTest {
*/
void _assertIdentical(
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
DartObjectImpl result = left.isIdentical(_typeProvider, right);
DartObjectImpl result = left.isIdentical2(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1681,10 +1704,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.integerDivide(_typeProvider, right);
left.integerDivide(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.integerDivide(_typeProvider, right);
DartObjectImpl result = left.integerDivide(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1699,10 +1722,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.lazyAnd(_typeProvider, () => right);
left.lazyAnd(_typeSystem, () => right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.lazyAnd(_typeProvider, () => right);
DartObjectImpl result = left.lazyAnd(_typeSystem, () => right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1717,10 +1740,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.lazyOr(_typeProvider, () => right);
left.lazyOr(_typeSystem, () => right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.lazyOr(_typeProvider, () => right);
DartObjectImpl result = left.lazyOr(_typeSystem, () => right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1735,10 +1758,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.lessThan(_typeProvider, right);
left.lessThan(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.lessThan(_typeProvider, right);
DartObjectImpl result = left.lessThan(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1753,10 +1776,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.lessThanOrEqual(_typeProvider, right);
left.lessThanOrEqual(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.lessThanOrEqual(_typeProvider, right);
DartObjectImpl result = left.lessThanOrEqual(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1769,10 +1792,10 @@ class DartObjectImplTest {
void _assertLogicalNot(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
expect(() {
operand.logicalNot(_typeProvider);
operand.logicalNot(_typeSystem);
}, throwsEvaluationException);
} else {
DartObjectImpl result = operand.logicalNot(_typeProvider);
DartObjectImpl result = operand.logicalNot(_typeSystem);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1787,10 +1810,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.minus(_typeProvider, right);
left.minus(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.minus(_typeProvider, right);
DartObjectImpl result = left.minus(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1803,10 +1826,10 @@ class DartObjectImplTest {
void _assertNegated(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
expect(() {
operand.negated(_typeProvider);
operand.negated(_typeSystem);
}, throwsEvaluationException);
} else {
DartObjectImpl result = operand.negated(_typeProvider);
DartObjectImpl result = operand.negated(_typeSystem);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1821,10 +1844,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.notEqual(_typeProvider, right);
left.notEqual(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.notEqual(_typeProvider, right);
DartObjectImpl result = left.notEqual(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1837,10 +1860,10 @@ class DartObjectImplTest {
void _assertPerformToString(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
expect(() {
operand.performToString(_typeProvider);
operand.performToString(_typeSystem);
}, throwsEvaluationException);
} else {
DartObjectImpl result = operand.performToString(_typeProvider);
DartObjectImpl result = operand.performToString(_typeSystem);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1855,10 +1878,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.remainder(_typeProvider, right);
left.remainder(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.remainder(_typeProvider, right);
DartObjectImpl result = left.remainder(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1873,10 +1896,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.shiftLeft(_typeProvider, right);
left.shiftLeft(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.shiftLeft(_typeProvider, right);
DartObjectImpl result = left.shiftLeft(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1891,10 +1914,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.shiftRight(_typeProvider, right);
left.shiftRight(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.shiftRight(_typeProvider, right);
DartObjectImpl result = left.shiftRight(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1907,10 +1930,10 @@ class DartObjectImplTest {
void _assertStringLength(DartObjectImpl expected, DartObjectImpl operand) {
if (expected == null) {
expect(() {
operand.stringLength(_typeProvider);
operand.stringLength(_typeSystem);
}, throwsEvaluationException);
} else {
DartObjectImpl result = operand.stringLength(_typeProvider);
DartObjectImpl result = operand.stringLength(_typeSystem);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1925,10 +1948,10 @@ class DartObjectImplTest {
DartObjectImpl expected, DartObjectImpl left, DartObjectImpl right) {
if (expected == null) {
expect(() {
left.times(_typeProvider, right);
left.times(_typeSystem, right);
}, throwsEvaluationException);
} else {
DartObjectImpl result = left.times(_typeProvider, right);
DartObjectImpl result = left.times(_typeSystem, right);
expect(result, isNotNull);
expect(result, expected);
}
@@ -1936,11 +1959,23 @@ class DartObjectImplTest {
DartObjectImpl _boolValue(bool value) {
if (value == null) {
return DartObjectImpl(_typeProvider.boolType, BoolState.UNKNOWN_VALUE);
return DartObjectImpl(
_typeSystem,
_typeProvider.boolType,
BoolState.UNKNOWN_VALUE,
);
} else if (identical(value, false)) {
return DartObjectImpl(_typeProvider.boolType, BoolState.FALSE_STATE);
return DartObjectImpl(
_typeSystem,
_typeProvider.boolType,
BoolState.FALSE_STATE,
);
} else if (identical(value, true)) {
return DartObjectImpl(_typeProvider.boolType, BoolState.TRUE_STATE);
return DartObjectImpl(
_typeSystem,
_typeProvider.boolType,
BoolState.TRUE_STATE,
);
}
fail("Invalid boolean value used in test");
}
@@ -1948,17 +1983,32 @@ class DartObjectImplTest {
DartObjectImpl _doubleValue(double value) {
if (value == null) {
return DartObjectImpl(
_typeProvider.doubleType, DoubleState.UNKNOWN_VALUE);
_typeSystem,
_typeProvider.doubleType,
DoubleState.UNKNOWN_VALUE,
);
} else {
return DartObjectImpl(_typeProvider.doubleType, DoubleState(value));
return DartObjectImpl(
_typeSystem,
_typeProvider.doubleType,
DoubleState(value),
);
}
}
DartObjectImpl _intValue(int value) {
if (value == null) {
return DartObjectImpl(_typeProvider.intType, IntState.UNKNOWN_VALUE);
return DartObjectImpl(
_typeSystem,
_typeProvider.intType,
IntState.UNKNOWN_VALUE,
);
} else {
return DartObjectImpl(_typeProvider.intType, IntState(value));
return DartObjectImpl(
_typeSystem,
_typeProvider.intType,
IntState(value),
);
}
}
@@ -1967,6 +2017,7 @@ class DartObjectImplTest {
List<DartObjectImpl> elements,
) {
return DartObjectImpl(
_typeSystem,
_typeProvider.listType2(elementType),
ListState(elements),
);
@@ -1981,29 +2032,49 @@ class DartObjectImplTest {
map[keyElementPairs[i++]] = keyElementPairs[i++];
}
return DartObjectImpl(
_typeSystem,
_typeProvider.mapType2(keyType, valueType),
MapState(map),
);
}
DartObjectImpl _nullValue() {
return DartObjectImpl(_typeProvider.nullType, NullState.NULL_STATE);
return DartObjectImpl(
_typeSystem,
_typeProvider.nullType,
NullState.NULL_STATE,
);
}
DartObjectImpl _setValue(DartType type, Set<DartObjectImpl> elements) {
return DartObjectImpl(type, SetState(elements ?? <DartObjectImpl>{}));
return DartObjectImpl(
_typeSystem,
type,
SetState(elements ?? <DartObjectImpl>{}),
);
}
DartObjectImpl _stringValue(String value) {
if (value == null) {
return DartObjectImpl(
_typeProvider.stringType, StringState.UNKNOWN_VALUE);
_typeSystem,
_typeProvider.stringType,
StringState.UNKNOWN_VALUE,
);
} else {
return DartObjectImpl(_typeProvider.stringType, StringState(value));
return DartObjectImpl(
_typeSystem,
_typeProvider.stringType,
StringState(value),
);
}
}
DartObjectImpl _symbolValue(String value) {
return DartObjectImpl(_typeProvider.symbolType, SymbolState(value));
return DartObjectImpl(
_typeSystem,
_typeProvider.symbolType,
SymbolState(value),
);
}
}
@@ -223,20 +223,6 @@ class ClassElementImplTest extends AbstractTypeTest {
expect(classA.hasStaticMember, isTrue);
}
void test_isEnum() {
String firstConst = "A";
String secondConst = "B";
EnumElementImpl enumE = ElementFactory.enumElement(
TestTypeProvider(), "E", [firstConst, secondConst]);
// E is an enum
expect(enumE.isEnum, true);
// A and B are static members
expect(enumE.getField(firstConst).isEnumConstant, true);
expect(enumE.getField(secondConst).isEnumConstant, true);
}
void test_lookUpConcreteMethod_declared() {
// class A {
// m() {}
@@ -867,28 +853,6 @@ class ClassElementImplTest extends AbstractTypeTest {
@reflectiveTest
class CompilationUnitElementImplTest {
void test_getEnum_declared() {
TestTypeProvider typeProvider = TestTypeProvider();
CompilationUnitElementImpl unit =
ElementFactory.compilationUnit("/lib.dart");
String enumName = "E";
ClassElement enumElement =
ElementFactory.enumElement(typeProvider, enumName);
unit.enums = <ClassElement>[enumElement];
expect(unit.getEnum(enumName), same(enumElement));
}
void test_getEnum_undeclared() {
TestTypeProvider typeProvider = TestTypeProvider();
CompilationUnitElementImpl unit =
ElementFactory.compilationUnit("/lib.dart");
String enumName = "E";
ClassElement enumElement =
ElementFactory.enumElement(typeProvider, enumName);
unit.enums = <ClassElement>[enumElement];
expect(unit.getEnum("${enumName}x"), isNull);
}
void test_getType_declared() {
CompilationUnitElementImpl unit =
ElementFactory.compilationUnit("/lib.dart");
@@ -17,6 +17,7 @@ import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/generated/type_system.dart';
import 'package:analyzer/src/test_utilities/find_element.dart';
import 'package:analyzer/src/test_utilities/find_node.dart';
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
@@ -80,6 +81,8 @@ mixin ResolutionTest implements ResourceProviderMixin {
TypeProvider get typeProvider => result.typeProvider;
TypeSystemImpl get typeSystem => result.typeSystem;
/// Whether `DartType.toString()` with nullability should be asked.
bool get typeToStringWithNullability => false;