All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.python.tree.UnaryExpressionImpl Maven / Gradle / Ivy

/*
 * SonarQube Python Plugin
 * Copyright (C) 2011-2023 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.python.tree;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.sonar.plugins.python.api.tree.Expression;
import org.sonar.plugins.python.api.tree.Token;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.plugins.python.api.tree.TreeVisitor;
import org.sonar.plugins.python.api.tree.UnaryExpression;
import org.sonar.plugins.python.api.types.InferredType;
import org.sonar.python.types.InferredTypes;

public class UnaryExpressionImpl extends PyTree implements UnaryExpression {

  private static final Map KINDS_BY_OPERATOR = kindsByOperator();

  private final Kind kind;
  private final Token operator;
  private final Expression expression;

  private static Map kindsByOperator() {
    Map map = new HashMap<>();
    map.put("+", Kind.UNARY_PLUS);
    map.put("-", Kind.UNARY_MINUS);
    map.put("~", Kind.BITWISE_COMPLEMENT);
    map.put("not", Kind.NOT);
    return map;
  }

  public UnaryExpressionImpl(Token operator, Expression expression) {
    this.kind = KINDS_BY_OPERATOR.get(operator.value());
    this.operator = operator;
    this.expression = expression;
  }

  @Override
  public Token operator() {
    return operator;
  }

  @Override
  public Expression expression() {
    return expression;
  }

  @Override
  public void accept(TreeVisitor visitor) {
    visitor.visitUnaryExpression(this);
  }

  @Override
  public List computeChildren() {
    return Stream.of(operator, expression).filter(Objects::nonNull).collect(Collectors.toList());
  }

  @Override
  public Kind getKind() {
    return kind;
  }

  @Override
  public InferredType type() {
    if (is(Kind.NOT)) {
      return InferredTypes.BOOL;
    }
    if (is(Kind.UNARY_MINUS, Kind.UNARY_PLUS)) {
      return expression.type();
    }
    return InferredTypes.anyType();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy