// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.syntax;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.devtools.build.lib.events.Location;
import com.google.devtools.build.lib.syntax.SkylarkList.MutableList;
import java.io.IOException;
import java.util.Collection;
/**
* A term that can appear on the left-hand side of an assignment statement, for loop, comprehension
* clause, etc. E.g.,
*
*
{@code lvalue = 2}
*
{@code [for lvalue in exp]}
*
{@code for lvalue in exp: pass}
*
*
*
An {@code LValue}'s expression must have one of the following forms:
*
*
(Variable assignment) an {@link Identifier};
*
(List or dictionary item assignment) an {@link IndexExpression}; or
*
(Sequence assignment) a non-empty {@link ListLiteral} (either list or tuple) of expressions
* that can themselves appear in an {@code LValue}.
*
* In particular and unlike Python, slice expressions, dot expressions, and starred expressions
* cannot appear in {@code LValue}s.
*/
// TODO(bazel-team): Add support for assigning to slices (e.g. a[2:6] = [3]).
public final class LValue extends ASTNode {
private final Expression expr;
public LValue(Expression expr) {
this.expr = expr;
setLocation(expr.getLocation());
}
public Expression getExpression() {
return expr;
}
/**
* Updates the environment bindings, and possibly mutates objects, so as to assign the given value
* to this {@code LValue}.
*/
public void assign(Object value, Environment env, Location loc)
throws EvalException, InterruptedException {
assign(expr, value, env, loc);
}
/**
* Updates the environment bindings, and possibly mutates objects, so as to assign the given
* value to the given expression. The expression must be valid for an {@code LValue}.
*/
private static void assign(Expression expr, Object value, Environment env, Location loc)
throws EvalException, InterruptedException {
if (expr instanceof Identifier) {
assignIdentifier((Identifier) expr, value, env, loc);
} else if (expr instanceof IndexExpression) {
Object object = ((IndexExpression) expr).getObject().eval(env);
Object key = ((IndexExpression) expr).getKey().eval(env);
assignItem(object, key, value, env, loc);
} else if (expr instanceof ListLiteral) {
ListLiteral list = (ListLiteral) expr;
assignList(list, value, env, loc);
} else {
// Not possible for validated ASTs.
throw new EvalException(loc, "cannot assign to '" + expr + "'");
}
}
/**
* Binds a variable to the given value in the environment.
*
* @throws EvalException if we're currently in a function's scope, and the identifier has
* previously resolved to a global variable in the same function
*/
private static void assignIdentifier(
Identifier ident, Object value, Environment env, Location loc)
throws EvalException, InterruptedException {
Preconditions.checkNotNull(value, "trying to assign null to %s", ident);
if (env.isKnownGlobalVariable(ident.getName())) {
throw new EvalException(
loc,
String.format(
"Variable '%s' is referenced before assignment. "
+ "The variable is defined in the global scope.",
ident.getName()));
}
env.update(ident.getName(), value);
}
/**
* Adds or changes an object-key-value relationship for a list or dict.
*
*
For a list, the key is an in-range index. For a dict, it is a hashable value.
*
* @throws EvalException if the object is not a list or dict
*/
@SuppressWarnings("unchecked")
private static void assignItem(
Object object, Object key, Object value, Environment env, Location loc)
throws EvalException, InterruptedException {
if (object instanceof SkylarkDict) {
SkylarkDict