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 | package com.google.devtools.build.lib.syntax; |
| 15 | |
Florian Weikert | a9dd72a | 2015-11-09 14:09:18 +0000 | [diff] [blame] | 16 | import com.google.devtools.build.lib.syntax.compiler.ByteCodeMethodCalls; |
| 17 | import com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils; |
| 18 | import com.google.devtools.build.lib.syntax.compiler.DebugInfo; |
| 19 | import com.google.devtools.build.lib.syntax.compiler.VariableScope; |
| 20 | |
| 21 | import net.bytebuddy.implementation.bytecode.ByteCodeAppender; |
| 22 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | /** |
| 24 | * As syntax node for the not boolean operation. |
| 25 | */ |
| 26 | public class NotExpression extends Expression { |
| 27 | |
| 28 | private final Expression expression; |
| 29 | |
| 30 | public NotExpression(Expression expression) { |
| 31 | this.expression = expression; |
| 32 | } |
| 33 | |
| 34 | Expression getExpression() { |
| 35 | return expression; |
| 36 | } |
| 37 | |
| 38 | @Override |
Florian Weikert | 90a1596 | 2015-09-11 13:43:10 +0000 | [diff] [blame] | 39 | Object doEval(Environment env) throws EvalException, InterruptedException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | return !EvalUtils.toBoolean(expression.eval(env)); |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public String toString() { |
| 45 | return "not " + expression; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void accept(SyntaxTreeVisitor visitor) { |
| 50 | visitor.visit(this); |
| 51 | } |
| 52 | |
| 53 | @Override |
Laurent Le Brun | 2e78d61 | 2015-04-15 09:06:46 +0000 | [diff] [blame] | 54 | void validate(ValidationEnvironment env) throws EvalException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | expression.validate(env); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | } |
Florian Weikert | a9dd72a | 2015-11-09 14:09:18 +0000 | [diff] [blame] | 57 | |
| 58 | @Override |
| 59 | ByteCodeAppender compile(VariableScope scope, DebugInfo debugInfo) throws EvalException { |
| 60 | // since there is no byte code logical negation |
| 61 | // compile expression and convert to boolean then negate and convert back to Boolean |
| 62 | return new ByteCodeAppender.Compound( |
| 63 | expression.compile(scope, debugInfo), |
| 64 | new ByteCodeAppender.Simple( |
| 65 | EvalUtils.toBoolean, |
| 66 | ByteCodeUtils.intLogicalNegation(), |
| 67 | ByteCodeMethodCalls.BCBoolean.valueOf)); |
| 68 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 69 | } |