24 package com.sun.tools.jextract.tree;
25
26 import java.foreign.layout.Address;
27 import java.foreign.layout.Function;
28 import java.foreign.layout.Group;
29 import java.foreign.layout.Layout;
30 import java.foreign.layout.Padding;
31 import java.foreign.layout.Sequence;
32 import java.foreign.layout.Unresolved;
33 import java.foreign.layout.Value;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.function.BiFunction;
37 import java.util.stream.Collectors;
38 import java.util.stream.Stream;
39 import jdk.internal.clang.Cursor;
40 import jdk.internal.clang.CursorKind;
41 import jdk.internal.clang.SourceLocation;
42 import jdk.internal.clang.Type;
43 import jdk.internal.clang.TypeKind;
44 import jdk.internal.foreign.memory.Types;
45
46 /**
47 * General Layout utility functions
48 */
49 public final class LayoutUtils {
50 private LayoutUtils() {}
51
52 public static String getName(Type type) {
53 Cursor c = type.getDeclarationCursor();
54 if (c.isInvalid()) {
55 return type.spelling();
56 }
57 return getName(c);
58 }
59
60 public static String getName(Tree tree) {
61 String name = tree.name();
62 return name.isEmpty()? getName(tree.cursor()) : name;
63 }
64
107 default:
108 throw new IllegalArgumentException(
109 "Unsupported type kind: " + t.kind());
110 }
111 }
112
113 private static Function parseFunctionInternal(Type t) {
114 final int argSize = t.numberOfArgs();
115 Layout[] args = new Layout[argSize];
116 for (int i = 0; i < argSize; i++) {
117 Layout l = getLayout(t.argType(i));
118 args[i] = l instanceof Sequence? Address.ofLayout(64, ((Sequence)l).element()) : l;
119 }
120 if (t.resultType().kind() == TypeKind.Void) {
121 return Function.ofVoid(t.isVariadic(), args);
122 } else {
123 return Function.of(getLayout(t.resultType()), t.isVariadic(), args);
124 }
125 }
126
127 public static Layout getLayout(Type t) {
128 switch(t.kind()) {
129 case Bool:
130 return Types.BOOLEAN;
131 case Int:
132 return Types.INT;
133 case UInt:
134 return Types.UNSIGNED.INT;
135 case Int128:
136 return Types.INT128;
137 case UInt128:
138 return Types.UNSIGNED.INT128;
139 case Short:
140 return Types.SHORT;
141 case UShort:
142 return Types.UNSIGNED.SHORT;
143 case Long:
144 return Types.LONG;
145 case ULong:
146 return Types.UNSIGNED.LONG;
|
24 package com.sun.tools.jextract.tree;
25
26 import java.foreign.layout.Address;
27 import java.foreign.layout.Function;
28 import java.foreign.layout.Group;
29 import java.foreign.layout.Layout;
30 import java.foreign.layout.Padding;
31 import java.foreign.layout.Sequence;
32 import java.foreign.layout.Unresolved;
33 import java.foreign.layout.Value;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.function.BiFunction;
37 import java.util.stream.Collectors;
38 import java.util.stream.Stream;
39 import jdk.internal.clang.Cursor;
40 import jdk.internal.clang.CursorKind;
41 import jdk.internal.clang.SourceLocation;
42 import jdk.internal.clang.Type;
43 import jdk.internal.clang.TypeKind;
44
45 /**
46 * General Layout utility functions
47 */
48 public final class LayoutUtils {
49 private LayoutUtils() {}
50
51 public static String getName(Type type) {
52 Cursor c = type.getDeclarationCursor();
53 if (c.isInvalid()) {
54 return type.spelling();
55 }
56 return getName(c);
57 }
58
59 public static String getName(Tree tree) {
60 String name = tree.name();
61 return name.isEmpty()? getName(tree.cursor()) : name;
62 }
63
106 default:
107 throw new IllegalArgumentException(
108 "Unsupported type kind: " + t.kind());
109 }
110 }
111
112 private static Function parseFunctionInternal(Type t) {
113 final int argSize = t.numberOfArgs();
114 Layout[] args = new Layout[argSize];
115 for (int i = 0; i < argSize; i++) {
116 Layout l = getLayout(t.argType(i));
117 args[i] = l instanceof Sequence? Address.ofLayout(64, ((Sequence)l).element()) : l;
118 }
119 if (t.resultType().kind() == TypeKind.Void) {
120 return Function.ofVoid(t.isVariadic(), args);
121 } else {
122 return Function.of(getLayout(t.resultType()), t.isVariadic(), args);
123 }
124 }
125
126 static class Types {
127 public final static Layout BYTE = Value.ofSignedInt(8);
128 public final static Layout SHORT = Value.ofSignedInt(16);
129 public final static Layout INT = Value.ofSignedInt(32);
130 public final static Layout LONG = Value.ofSignedInt(64);
131 public final static Layout LONG_LONG = Value.ofSignedInt(64);
132 public final static Layout FLOAT = Value.ofFloatingPoint(32);
133 public final static Layout DOUBLE = Value.ofFloatingPoint(64);
134 public final static Layout LONG_DOUBLE = Value.ofFloatingPoint(128);
135 public final static Layout CHAR = Value.ofSignedInt(8);
136 public final static Layout BOOLEAN = Value.ofUnsignedInt(8);
137 public final static Layout POINTER = Address.ofVoid(64);
138 public final static Layout INT8 = Value.ofSignedInt(8);
139 public final static Layout INT16 = Value.ofSignedInt(16);
140 public final static Layout INT32 = Value.ofSignedInt(32);
141 public final static Layout INT64 = Value.ofSignedInt(64);
142 public final static Layout INT128 = Value.ofSignedInt(128);
143 public final static Layout VOID = Value.ofUnsignedInt(0);
144
145 public static class UNSIGNED {
146 public final static Layout BYTE = Value.ofUnsignedInt(8);
147 public final static Layout SHORT = Value.ofUnsignedInt(16);
148 public final static Layout INT = Value.ofUnsignedInt(32);
149 public final static Layout LONG = Value.ofUnsignedInt(64);
150 public final static Layout LONG_LONG = Value.ofUnsignedInt(64);
151 public final static Layout INT8 = Value.ofUnsignedInt(8);
152 public final static Layout INT16 = Value.ofUnsignedInt(16);
153 public final static Layout INT32 = Value.ofUnsignedInt(32);
154 public final static Layout INT64 = Value.ofUnsignedInt(64);
155 public final static Layout INT128 = Value.ofUnsignedInt(128);
156 }
157 }
158
159 public static Layout getLayout(Type t) {
160 switch(t.kind()) {
161 case Bool:
162 return Types.BOOLEAN;
163 case Int:
164 return Types.INT;
165 case UInt:
166 return Types.UNSIGNED.INT;
167 case Int128:
168 return Types.INT128;
169 case UInt128:
170 return Types.UNSIGNED.INT128;
171 case Short:
172 return Types.SHORT;
173 case UShort:
174 return Types.UNSIGNED.SHORT;
175 case Long:
176 return Types.LONG;
177 case ULong:
178 return Types.UNSIGNED.LONG;
|