001/*
002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package gololang.ir;
012
013import java.util.Collections;
014import java.util.List;
015
016public final class UnaryOperation extends ExpressionStatement<UnaryOperation> {
017
018  private final OperatorType type;
019  private ExpressionStatement<?> expressionStatement;
020
021  UnaryOperation(OperatorType type, ExpressionStatement<?> expressionStatement) {
022    super();
023    this.type = type;
024    setExpressionStatement(expressionStatement);
025  }
026
027  /**
028   * Creates a generic unary operation.
029   */
030  public static UnaryOperation create(Object type, Object expression) {
031    return new UnaryOperation(OperatorType.of(type), ExpressionStatement.of(expression));
032  }
033
034  protected UnaryOperation self() { return this; }
035
036  public ExpressionStatement<?> expression() {
037    return expressionStatement;
038  }
039
040  private void setExpressionStatement(ExpressionStatement<?> statement) {
041    this.expressionStatement = makeParentOf(statement);
042  }
043
044  public OperatorType getType() {
045    return type;
046  }
047
048  /**
049   * {@inheritDoc}
050   */
051  @Override
052  public void accept(GoloIrVisitor visitor) {
053    visitor.visitUnaryOperation(this);
054  }
055
056  /**
057   * {@inheritDoc}
058   */
059  @Override
060  public List<GoloElement<?>> children() {
061    return Collections.singletonList(expressionStatement);
062  }
063
064  /**
065   * {@inheritDoc}
066   */
067  @Override
068  protected void replaceElement(GoloElement<?> original, GoloElement<?> newElement) {
069    if (expressionStatement.equals(original)) {
070      setExpressionStatement(ExpressionStatement.of(newElement));
071    } else {
072      throw cantReplace(original, newElement);
073    }
074  }
075}