1 /*
  2  * Copyright (c) 2025, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package optkl.ifacemapper;
 26 
 27 import jdk.incubator.code.Value;
 28 import jdk.incubator.code.dialect.java.JavaType;
 29 import optkl.OpHelper;
 30 
 31 import java.lang.annotation.Annotation;
 32 import java.lang.invoke.MethodHandles;
 33 
 34 public enum AccessType {
 35     NOT_BUFFER((byte) 0),
 36     NA((byte) 1),
 37     RO((byte) 2),
 38     WO((byte) 4),
 39     RW((byte) 6);
 40 
 41     public final byte value;
 42 
 43     AccessType(byte i) {
 44         value = i;
 45     }
 46 
 47     public static AccessType of(byte i) {
 48         return switch (i) {
 49             case (byte)0 -> NOT_BUFFER;
 50             case (byte)1 -> NA;
 51             case (byte)2 -> RO;
 52             case (byte)4 -> WO;
 53             case (byte)6 -> RO;
 54             default -> throw new IllegalStateException("No access type for " + i);
 55         };
 56     }
 57 
 58     public static AccessType of(Annotation annotation) {
 59         return switch (annotation) {
 60             case MappableIface.RO ro -> RO;
 61             case MappableIface.RW rw -> RW;
 62             case MappableIface.WO wo -> WO;
 63             default -> NA;
 64         };
 65     }
 66 
 67 
 68 
 69     public record TypeAndAccess(Annotation[] annotations, Value value, JavaType javaType) {
 70         public static TypeAndAccess of(Annotation[] annotations, Value value) {
 71             return new TypeAndAccess(annotations, value, (JavaType) value.type());
 72         }
 73 
 74       public  boolean isIface(MethodHandles.Lookup lookup) {
 75             return OpHelper.isAssignable(lookup, javaType, MappableIface.class);
 76         }
 77 
 78       public   boolean ro() {
 79             for (Annotation annotation : annotations) {
 80                 if (annotation instanceof MappableIface.RO) {
 81                     return true;
 82                 }
 83             }
 84             return false;
 85         }
 86         public boolean mutatesBuffer(){
 87             return rw()||wo();
 88         }
 89 
 90        public boolean rw() {
 91             for (Annotation annotation : annotations) {
 92                 if (annotation instanceof MappableIface.RW) {
 93                     return true;
 94                 }
 95             }
 96             return false;
 97         }
 98 
 99        public boolean wo() {
100             for (Annotation annotation : annotations) {
101                 if (annotation instanceof MappableIface.WO) {
102                     return true;
103                 }
104             }
105             return false;
106         }
107     }
108 
109 }