1 /*
   2  * Copyright (c) 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  * Represents the type of {@link Value values}. This class can be extended by compilers to track
  27  * additional information about values.
  28  */
  29 public abstract class ValueKind<K extends ValueKind<K>> {
  30 
  31     private enum IllegalKind implements PlatformKind {
  32         ILLEGAL;
  33 
  34         private final EnumKey<IllegalKind> key = new EnumKey<>(this);
  35 
  36         public Key getKey() {
  37             return key;
  38         }
  39 
  40         public int getSizeInBytes() {
  41             return 0;
  42         }
  43 
  44         public int getVectorLength() {
  45             return 0;
  46         }
  47 
  48         public char getTypeChar() {
  49             return '-';
  50         }
  51     }
  52 
  53     private static class IllegalValueKind extends ValueKind<IllegalValueKind> {
  54 
  55         IllegalValueKind() {
  56             super(IllegalKind.ILLEGAL);
  57         }
  58 
  59         @Override
  60         public IllegalValueKind changeType(PlatformKind newPlatformKind) {
  61             return this;
  62         }
  63 
  64         @Override
  65         public String toString() {
  66             return "ILLEGAL";
  67         }
  68     }
  69 
  70     /**
  71      * The non-type.
  72      */
  73     public static final ValueKind<?> Illegal = new IllegalValueKind();
  74 
  75     private final PlatformKind platformKind;
  76 
  77     public ValueKind(PlatformKind platformKind) {
  78         this.platformKind = platformKind;
  79     }
  80 
  81     public final PlatformKind getPlatformKind() {
  82         return platformKind;
  83     }
  84 
  85     /**
  86      * Create a new {@link ValueKind} with a different {@link PlatformKind}. Subclasses must
  87      * override this to preserve the additional information added by the compiler.
  88      */
  89     public abstract K changeType(PlatformKind newPlatformKind);
  90 
  91     /**
  92      * Returns a String representation of the kind, which will be included at the end of
  93      * {@link Value#toString()} implementation. Defaults to {@link #toString()} but can be
  94      * overridden to provide something more specific.
  95      */
  96     public String getKindSuffix() {
  97         return toString();
  98     }
  99 }