Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 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 | |
| 15 | package com.google.devtools.build.lib.syntax; |
| 16 | |
Florian Weikert | db8b867 | 2015-11-09 13:26:24 +0000 | [diff] [blame] | 17 | import com.google.common.base.Optional; |
| 18 | import com.google.devtools.build.lib.syntax.compiler.DebugInfo; |
| 19 | import com.google.devtools.build.lib.syntax.compiler.LoopLabels; |
| 20 | import com.google.devtools.build.lib.syntax.compiler.VariableScope; |
| 21 | |
| 22 | import net.bytebuddy.implementation.bytecode.ByteCodeAppender; |
| 23 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | /** |
| 25 | * Syntax node for an assignment statement. |
| 26 | */ |
| 27 | public final class AssignmentStatement extends Statement { |
| 28 | |
Laurent Le Brun | 0242382 | 2015-03-19 10:12:49 +0000 | [diff] [blame] | 29 | private final LValue lvalue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | |
| 31 | private final Expression expression; |
| 32 | |
| 33 | /** |
| 34 | * Constructs an assignment: "lvalue := value". |
| 35 | */ |
| 36 | AssignmentStatement(Expression lvalue, Expression expression) { |
Laurent Le Brun | 0242382 | 2015-03-19 10:12:49 +0000 | [diff] [blame] | 37 | this.lvalue = new LValue(lvalue); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | this.expression = expression; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Returns the LHS of the assignment. |
| 43 | */ |
Laurent Le Brun | 0242382 | 2015-03-19 10:12:49 +0000 | [diff] [blame] | 44 | public LValue getLValue() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | 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 Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 61 | void doExec(Environment env) throws EvalException, InterruptedException { |
Laurent Le Brun | 5321c3c | 2015-03-19 11:51:58 +0000 | [diff] [blame] | 62 | Object rvalue = expression.eval(env); |
| 63 | lvalue.assign(env, getLocation(), rvalue); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 64 | } |
| 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 Brun | 964d8d5 | 2015-04-13 12:15:04 +0000 | [diff] [blame] | 73 | expression.validate(env); |
| 74 | lvalue.validate(env, getLocation()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | } |
Florian Weikert | db8b867 | 2015-11-09 13:26:24 +0000 | [diff] [blame] | 76 | |
| 77 | @Override |
| 78 | ByteCodeAppender compile( |
| 79 | VariableScope scope, Optional<LoopLabels> loopLabels, DebugInfo debugInfo) |
Florian Weikert | 33f819b | 2015-11-09 18:15:55 +0000 | [diff] [blame] | 80 | throws EvalException { |
Florian Weikert | db8b867 | 2015-11-09 13:26:24 +0000 | [diff] [blame] | 81 | return new ByteCodeAppender.Compound( |
| 82 | expression.compile(scope, debugInfo), |
| 83 | lvalue.compileAssignment(this, debugInfo.add(this), scope)); |
| 84 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | } |