001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.compilers.opt.ir.operand;
014
015import org.jikesrvm.classloader.RVMType;
016import org.jikesrvm.classloader.TypeReference;
017
018/**
019 * A TypeOperand represents a type.
020 * Used in checkcast, instanceof, new, etc.
021 * It will contain either a RVMType (if the type can be resolved
022 * at compile time) or a TypeReference (if the type cannot be resolved
023 * at compile time).
024 *
025 * @see Operand
026 * @see RVMType
027 * @see TypeReference
028 */
029public final class TypeOperand extends Operand {
030
031  /**
032   * A type
033   */
034  private final RVMType type;
035
036  /**
037   * The data type.
038   */
039  private final TypeReference typeRef;
040
041  public TypeOperand(RVMType typ) {
042    type = typ;
043    typeRef = type.getTypeRef();
044  }
045
046  public TypeOperand(TypeReference tr) {
047    type = tr.peekType();
048    typeRef = tr;
049  }
050
051  private TypeOperand(RVMType t, TypeReference tr) {
052    type = t;
053    typeRef = tr;
054  }
055
056  @Override
057  public TypeReference getType() {
058    return TypeReference.Type;
059  }
060
061  /**
062   * @return the TypeReference for this type operand
063   */
064  public TypeReference getTypeRef() {
065    return typeRef;
066  }
067
068  /**
069   * @return the RVMType for this type operand -- may be null
070   */
071  public RVMType getVMType() {
072    if (type != null)
073      return type;
074    else
075      return typeRef.peekType();
076  }
077
078  @Override
079  public Operand copy() {
080    return new TypeOperand(type, typeRef);
081  }
082
083  @Override
084  public boolean similar(Operand op) {
085    if (op instanceof TypeOperand) {
086      TypeOperand that = (TypeOperand) op;
087      return type == that.type && typeRef == that.typeRef;
088    } else {
089      return false;
090    }
091  }
092
093  /**
094   * Returns the string representation of this operand.
095   *
096   * @return a string representation of this operand.
097   */
098  @Override
099  public String toString() {
100    if (type != null) {
101      return type.toString();
102    } else {
103      return typeRef.getName().toString();
104    }
105  }
106}