1 /*
  2  * Copyright (c) 2020, 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 
 26 package jdk.internal.jextract.impl;
 27 
 28 import jdk.incubator.foreign.ValueLayout;
 29 import jdk.incubator.jextract.Type.Primitive;
 30 import jdk.incubator.jextract.Type;
 31 import jdk.incubator.foreign.MemoryAddress;
 32 import jdk.incubator.foreign.MemoryLayout;
 33 import jdk.incubator.foreign.MemorySegment;
 34 
 35 import java.lang.invoke.MethodType;
 36 
 37 public class TypeTranslator implements Type.Visitor<Class<?>, Void> {
 38     @Override
 39     public Class<?> visitPrimitive(Type.Primitive t, Void aVoid) {
 40         if (t.kind().layout().isEmpty()) {
 41             return void.class;
 42         } else {
 43             return layoutToClass(isFloatingPoint(t), t.kind().layout().orElseThrow(UnsupportedOperationException::new));
 44         }
 45     }
 46 
 47     private boolean isFloatingPoint(Type.Primitive t) {
 48         switch (t.kind()) {
 49             case Float:
 50             case Float128:
 51             case HalfFloat:
 52             case Double:
 53             case LongDouble:
 54                 return true;
 55             default:
 56                 return false;
 57         }
 58     }
 59 
 60     static String typeToLayoutName(Primitive.Kind type) {
 61         return Utils.layoutToConstant((ValueLayout)type.layout().orElseThrow());
 62     }
 63 
 64     static Class<?> layoutToClass(boolean fp, MemoryLayout layout) {
 65         switch ((int)layout.bitSize()) {
 66             case 8: return byte.class;
 67             case 16: return short.class;
 68             case 32: return !fp ? int.class : float.class;
 69             case 64:
 70             case 128: return !fp ? long.class : double.class;
 71             default:
 72                 throw new UnsupportedOperationException("size: " + (int)layout.bitSize());
 73         }
 74     }
 75 
 76     @Override
 77     public Class<?> visitDelegated(Type.Delegated t, Void aVoid) {
 78         return t.kind() == Type.Delegated.Kind.POINTER ?
 79                 MemoryAddress.class :
 80                 t.type().accept(this, null);
 81     }
 82 
 83     @Override
 84     public Class<?> visitFunction(Type.Function t, Void aVoid) {
 85         return MemoryAddress.class; // function pointer
 86     }
 87 
 88     @Override
 89     public Class<?> visitDeclared(Type.Declared t, Void aVoid) {
 90         switch (t.tree().kind()) {
 91             case UNION:
 92             case STRUCT:
 93                 return MemorySegment.class;
 94             case ENUM:
 95                 return layoutToClass(false, t.tree().layout().orElseThrow(UnsupportedOperationException::new));
 96             default:
 97                 throw new UnsupportedOperationException("declaration kind: " + t.tree().kind());
 98         }
 99     }
100 
101     @Override
102     public Class<?> visitArray(Type.Array t, Void aVoid) {
103         if (t.kind() == Type.Array.Kind.VECTOR) {
104             throw new UnsupportedOperationException("vector");
105         } else {
106             return MemorySegment.class;
107         }
108     }
109 
110     @Override
111     public Class<?> visitType(Type t, Void aVoid) {
112         throw new UnsupportedOperationException(t.getClass().toString());
113     }
114 
115     Class<?> getJavaType(Type t) {
116         return t.accept(this, null);
117     }
118 
119     MethodType getMethodType(Type.Function type) {
120         return getMethodType(type, true);
121     }
122 
123     MethodType getMethodType(Type.Function type, boolean varargsCheck) {
124         MethodType mtype = MethodType.methodType(getJavaType(type.returnType()));
125         for (Type arg : type.argumentTypes()) {
126             mtype = mtype.appendParameterTypes(getJavaType(arg));
127         }
128         if (varargsCheck && type.varargs()) {
129             mtype = mtype.appendParameterTypes(Object[].class);
130         }
131         return mtype;
132     }
133 }