blob: 75b93e755321c9735c1ba4a59fccc3e68068e4e7 [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.
14package com.google.devtools.build.lib.syntax;
15
Florian Weikerta9dd72a2015-11-09 14:09:18 +000016import com.google.devtools.build.lib.syntax.compiler.ByteCodeMethodCalls;
17import com.google.devtools.build.lib.syntax.compiler.ByteCodeUtils;
18import com.google.devtools.build.lib.syntax.compiler.DebugInfo;
19import com.google.devtools.build.lib.syntax.compiler.VariableScope;
20
21import net.bytebuddy.implementation.bytecode.ByteCodeAppender;
22
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023/**
24 * As syntax node for the not boolean operation.
25 */
26public 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 Weikert90a15962015-09-11 13:43:10 +000039 Object doEval(Environment env) throws EvalException, InterruptedException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040 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 Brun2e78d612015-04-15 09:06:46 +000054 void validate(ValidationEnvironment env) throws EvalException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010055 expression.validate(env);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056 }
Florian Weikerta9dd72a2015-11-09 14:09:18 +000057
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 Nienhuysd08b27f2015-02-25 16:45:20 +010069}