1 /*
   2  * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package jdk.vm.ci.meta;
  24 
  25 /**
  26  * Abstract base class for values.
  27  */
  28 public abstract class Value {
  29 
  30     public static final Value[] NO_VALUES = new Value[0];
  31 
  32     public static final AllocatableValue ILLEGAL = new IllegalValue();
  33 
  34     private static final class IllegalValue extends AllocatableValue {
  35         private IllegalValue() {
  36             super(ValueKind.Illegal);
  37         }
  38 
  39         @Override
  40         public String toString() {
  41             return "-";
  42         }
  43 
  44         @Override
  45         public boolean equals(Object other) {
  46             // Due to de-serialization this object may exist multiple times. So we compare classes
  47             // instead of the individual objects. (This anonymous class has always the same meaning)
  48             return other instanceof IllegalValue;
  49         }
  50     }
  51 
  52     private final ValueKind<?> valueKind;
  53 
  54     /**
  55      * Initializes a new value of the specified kind.
  56      *
  57      * @param valueKind the kind
  58      */
  59     protected Value(ValueKind<?> valueKind) {
  60         this.valueKind = valueKind;
  61     }
  62 
  63     /**
  64      * Returns a String representation of the kind, which should be the end of all
  65      * {@link #toString()} implementation of subclasses.
  66      */
  67     protected final String getKindSuffix() {
  68         return "|" + valueKind.getKindSuffix();
  69     }
  70 
  71     public final ValueKind<?> getValueKind() {
  72         return valueKind;
  73     }
  74 
  75     public final <K extends ValueKind<K>> K getValueKind(Class<K> cls) {
  76         return cls.cast(valueKind);
  77     }
  78 
  79     /**
  80      * Returns the platform specific kind used to store this value.
  81      */
  82     public final PlatformKind getPlatformKind() {
  83         return valueKind.getPlatformKind();
  84     }
  85 
  86     @Override
  87     public int hashCode() {
  88         return 41 + valueKind.hashCode();
  89     }
  90 
  91     @Override
  92     public boolean equals(Object obj) {
  93         if (obj instanceof Value) {
  94             Value that = (Value) obj;
  95             return valueKind.equals(that.valueKind);
  96         }
  97         return false;
  98     }
  99 
 100     /**
 101      * Checks if this value is identical to {@code other}.
 102      *
 103      * Warning: Use with caution! Usually equivalence {@link #equals(Object)} is sufficient and
 104      * should be used.
 105      */
 106     public final boolean identityEquals(Value other) {
 107         return this == other;
 108     }
 109 }