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.Addressable;
 29 import jdk.incubator.foreign.ValueLayout;
 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<?>, Boolean> {
 38     @Override
 39     public Class<?> visitPrimitive(Type.Primitive t, Boolean isArg) {
 40         if (t.kind().layout().isEmpty()) {
 41             return void.class;
 42         } else {
 43             return layoutToClass(t.kind().layout().orElseThrow(UnsupportedOperationException::new));
 44         }
 45     }
 46 
 47     static Class<?> layoutToClass(MemoryLayout layout) {
 48         if (layout instanceof ValueLayout valueLayout) {
 49             return valueLayout.carrier();
 50         } else {
 51             throw new UnsupportedOperationException("size: " + (int)layout.bitSize());
 52         }
 53     }
 54 
 55     @Override
 56     public Class<?> visitDelegated(Type.Delegated t, Boolean isArg) {
 57         return t.kind() == Type.Delegated.Kind.POINTER ?
 58                 (isArg ? Addressable.class : MemoryAddress.class) :
 59                 t.type().accept(this, isArg);
 60     }
 61 
 62     @Override
 63     public Class<?> visitFunction(Type.Function t, Boolean isArg) {
 64         return isArg ? Addressable.class : MemoryAddress.class; // function pointer
 65     }
 66 
 67     @Override
 68     public Class<?> visitDeclared(Type.Declared t, Boolean isArg) {
 69         return switch (t.tree().kind()) {
 70             case UNION, STRUCT -> MemorySegment.class;
 71             case ENUM -> layoutToClass(t.tree().layout().orElseThrow(UnsupportedOperationException::new));
 72             default -> throw new UnsupportedOperationException("declaration kind: " + t.tree().kind());
 73         };
 74     }
 75 
 76     @Override
 77     public Class<?> visitArray(Type.Array t, Boolean isArg) {
 78         if (t.kind() == Type.Array.Kind.VECTOR) {
 79             throw new UnsupportedOperationException("vector");
 80         } else {
 81             return MemorySegment.class;
 82         }
 83     }
 84 
 85     @Override
 86     public Class<?> visitType(Type t, Boolean isArg) {
 87         throw new UnsupportedOperationException(t.getClass().toString());
 88     }
 89 
 90     Class<?> getJavaType(Type t, boolean isArg) {
 91         return t.accept(this, isArg);
 92     }
 93 
 94     MethodType getMethodType(Type.Function type, boolean downcall) {
 95         MethodType mtype = MethodType.methodType(getJavaType(type.returnType(), !downcall));
 96         for (Type arg : type.argumentTypes()) {
 97             mtype = mtype.appendParameterTypes(getJavaType(arg, downcall));
 98         }
 99         if (downcall && type.varargs()) {
100             mtype = mtype.appendParameterTypes(Object[].class);
101         }
102         return mtype;
103     }
104 }