blob: 2b80e7ba31f38ad06729633458813d6577490c4e [file] [log] [blame]
// Copyright (c) 2014, 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/constant/value.dart';
import 'package:analyzer/dart/element/type_provider.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.
///
/// Clients may not extend, implement or mix-in this class.
class DeclaredVariables {
/// A table mapping the names of declared variables to their values.
final Map<String, String> _declaredVariables = <String, String>{};
/// Initialize a newly created set of declared variables in which there are no
/// variables.
DeclaredVariables();
/// Initialize a newly created set of declared variables to define variables
/// whose names are the keys in the give [variableMap] and whose values are
/// the corresponding values from the map.
DeclaredVariables.fromMap(Map<String, String> variableMap) {
_declaredVariables.addAll(variableMap);
}
/// Return the names of the variables for which a value has been defined.
Iterable<String> get variableNames => _declaredVariables.keys;
/// Add all variables of [other] to this object.
@deprecated
void addAll(DeclaredVariables other) {
_declaredVariables.addAll(other._declaredVariables);
}
/// Define a variable with the given [name] to have the given [value].
@deprecated
void define(String name, String value) {
_declaredVariables[name] = value;
}
/// Return the raw string value of the variable with the given [name],
/// or `null` of the variable is not defined.
String get(String name) => _declaredVariables[name];
/// 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. 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) {
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) {
return _evaluator(typeProvider).getInt(name);
}
/// 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. 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) {
return _evaluator(typeProvider).getString(name);
}
FromEnvironmentEvaluator _evaluator(TypeProvider typeProvider) {
var typeSystem = TypeSystemImpl(
implicitCasts: false,
isNonNullableByDefault: false,
strictInference: false,
typeProvider: typeProvider,
);
return FromEnvironmentEvaluator(typeSystem, this);
}
}