25
25
26
26
package jdk .internal .jextract .impl ;
27
27
28
+ import jdk .incubator .foreign .Addressable ;
28
29
import jdk .incubator .foreign .ValueLayout ;
29
30
import jdk .incubator .jextract .Type .Primitive ;
30
31
import jdk .incubator .jextract .Type ;
34
35
35
36
import java .lang .invoke .MethodType ;
36
37
37
- public class TypeTranslator implements Type .Visitor <Class <?>, Void > {
38
+ public class TypeTranslator implements Type .Visitor <Class <?>, Boolean > {
38
39
@ Override
39
- public Class <?> visitPrimitive (Type .Primitive t , Void aVoid ) {
40
+ public Class <?> visitPrimitive (Type .Primitive t , Boolean isArg ) {
40
41
if (t .kind ().layout ().isEmpty ()) {
41
42
return void .class ;
42
43
} else {
@@ -74,32 +75,28 @@ static Class<?> layoutToClass(boolean fp, MemoryLayout layout) {
74
75
}
75
76
76
77
@ Override
77
- public Class <?> visitDelegated (Type .Delegated t , Void aVoid ) {
78
+ public Class <?> visitDelegated (Type .Delegated t , Boolean isArg ) {
78
79
return t .kind () == Type .Delegated .Kind .POINTER ?
79
- MemoryAddress .class :
80
- t .type ().accept (this , null );
80
+ ( isArg ? Addressable . class : MemoryAddress .class ) :
81
+ t .type ().accept (this , isArg );
81
82
}
82
83
83
84
@ Override
84
- public Class <?> visitFunction (Type .Function t , Void aVoid ) {
85
- return MemoryAddress .class ; // function pointer
85
+ public Class <?> visitFunction (Type .Function t , Boolean isArg ) {
86
+ return isArg ? Addressable . class : MemoryAddress .class ; // function pointer
86
87
}
87
88
88
89
@ 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
- }
90
+ public Class <?> visitDeclared (Type .Declared t , Boolean isArg ) {
91
+ return switch (t .tree ().kind ()) {
92
+ case UNION , STRUCT -> MemorySegment .class ;
93
+ case ENUM -> layoutToClass (false , t .tree ().layout ().orElseThrow (UnsupportedOperationException ::new ));
94
+ default -> throw new UnsupportedOperationException ("declaration kind: " + t .tree ().kind ());
95
+ };
99
96
}
100
97
101
98
@ Override
102
- public Class <?> visitArray (Type .Array t , Void aVoid ) {
99
+ public Class <?> visitArray (Type .Array t , Boolean isArg ) {
103
100
if (t .kind () == Type .Array .Kind .VECTOR ) {
104
101
throw new UnsupportedOperationException ("vector" );
105
102
} else {
@@ -108,24 +105,20 @@ public Class<?> visitArray(Type.Array t, Void aVoid) {
108
105
}
109
106
110
107
@ Override
111
- public Class <?> visitType (Type t , Void aVoid ) {
108
+ public Class <?> visitType (Type t , Boolean isArg ) {
112
109
throw new UnsupportedOperationException (t .getClass ().toString ());
113
110
}
114
111
115
- Class <?> getJavaType (Type t ) {
116
- return t .accept (this , null );
117
- }
118
-
119
- MethodType getMethodType (Type .Function type ) {
120
- return getMethodType (type , true );
112
+ Class <?> getJavaType (Type t , boolean isArg ) {
113
+ return t .accept (this , isArg );
121
114
}
122
115
123
- MethodType getMethodType (Type .Function type , boolean varargsCheck ) {
124
- MethodType mtype = MethodType .methodType (getJavaType (type .returnType ()));
116
+ MethodType getMethodType (Type .Function type , boolean downcall ) {
117
+ MethodType mtype = MethodType .methodType (getJavaType (type .returnType (), ! downcall ));
125
118
for (Type arg : type .argumentTypes ()) {
126
- mtype = mtype .appendParameterTypes (getJavaType (arg ));
119
+ mtype = mtype .appendParameterTypes (getJavaType (arg , downcall ));
127
120
}
128
- if (varargsCheck && type .varargs ()) {
121
+ if (downcall && type .varargs ()) {
129
122
mtype = mtype .appendParameterTypes (Object [].class );
130
123
}
131
124
return mtype ;
0 commit comments