blob: ed4b1273326c32dc18b15a174845c7061b69aa58 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.syntax;
16
Florian Weikertdb8b8672015-11-09 13:26:24 +000017import com.google.common.base.Optional;
18import com.google.devtools.build.lib.syntax.compiler.DebugInfo;
19import com.google.devtools.build.lib.syntax.compiler.LoopLabels;
20import com.google.devtools.build.lib.syntax.compiler.VariableScope;
21
22import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
23
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024/**
25 * Syntax node for an assignment statement.
26 */
27public final class AssignmentStatement extends Statement {
28
Laurent Le Brun02423822015-03-19 10:12:49 +000029 private final LValue lvalue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030
31 private final Expression expression;
32
33 /**
34 * Constructs an assignment: "lvalue := value".
35 */
36 AssignmentStatement(Expression lvalue, Expression expression) {
Laurent Le Brun02423822015-03-19 10:12:49 +000037 this.lvalue = new LValue(lvalue);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038 this.expression = expression;
39 }
40
41 /**
42 * Returns the LHS of the assignment.
43 */
Laurent Le Brun02423822015-03-19 10:12:49 +000044 public LValue getLValue() {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045 return lvalue;
46 }
47
48 /**
49 * Returns the RHS of the assignment.
50 */
51 public Expression getExpression() {
52 return expression;
53 }
54
55 @Override
56 public String toString() {
57 return lvalue + " = " + expression + '\n';
58 }
59
60 @Override
Florian Weikert90a15962015-09-11 13:43:10 +000061 void doExec(Environment env) throws EvalException, InterruptedException {
Laurent Le Brun5321c3c2015-03-19 11:51:58 +000062 Object rvalue = expression.eval(env);
63 lvalue.assign(env, getLocation(), rvalue);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010064 }
65
66 @Override
67 public void accept(SyntaxTreeVisitor visitor) {
68 visitor.visit(this);
69 }
70
71 @Override
72 void validate(ValidationEnvironment env) throws EvalException {
Laurent Le Brun964d8d52015-04-13 12:15:04 +000073 expression.validate(env);
74 lvalue.validate(env, getLocation());
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010075 }
Florian Weikertdb8b8672015-11-09 13:26:24 +000076
77 @Override
78 ByteCodeAppender compile(
79 VariableScope scope, Optional<LoopLabels> loopLabels, DebugInfo debugInfo)
Florian Weikert33f819b2015-11-09 18:15:55 +000080 throws EvalException {
Florian Weikertdb8b8672015-11-09 13:26:24 +000081 return new ByteCodeAppender.Compound(
82 expression.compile(scope, debugInfo),
83 lvalue.compileAssignment(this, debugInfo.add(this), scope));
84 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010085}