|
| 1 | +package com.falsepattern.jwin32.conversion; |
| 2 | + |
| 3 | +import com.falsepattern.jwin32.conversion.common.*; |
| 4 | +import jdk.incubator.foreign.GroupLayout; |
| 5 | +import jdk.incubator.foreign.MemoryLayout; |
| 6 | +import jdk.incubator.foreign.ValueLayout; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.List; |
| 11 | +import java.util.stream.Stream; |
| 12 | + |
| 13 | +public class Struct { |
| 14 | + private static final CField SEGMENT_FIELD = new CField(); |
| 15 | + private static final CConstructor SUPER_CALL_ASSIGN = new CConstructor(); |
| 16 | + private static final CConstructor SUPER_CALL_ALLOCATOR = new CConstructor(); |
| 17 | + static { |
| 18 | + SEGMENT_FIELD.accessSpecifier.pub = true; |
| 19 | + SEGMENT_FIELD.accessSpecifier.fin = true; |
| 20 | + SEGMENT_FIELD.name = "segment"; |
| 21 | + SEGMENT_FIELD.type = CType.MEMORY_SEGMENT; |
| 22 | + |
| 23 | + SUPER_CALL_ALLOCATOR.accessSpecifier.pub = true; |
| 24 | + SUPER_CALL_ALLOCATOR.paramList.add(new CParameter(CType.SEGMENT_ALLOCATOR, "allocator")); |
| 25 | + SUPER_CALL_ALLOCATOR.code.append("super(allocator);"); |
| 26 | + |
| 27 | + SUPER_CALL_ASSIGN.accessSpecifier.pub = true; |
| 28 | + SUPER_CALL_ASSIGN.paramList.add(new CParameter(CType.MEMORY_SEGMENT, "segment")); |
| 29 | + SUPER_CALL_ASSIGN.code.append("super(segment);"); |
| 30 | + |
| 31 | + } |
| 32 | + public final CClass implementation = new CClass(); |
| 33 | + private final GroupLayout layout; |
| 34 | + public final Class<?> parent; |
| 35 | + private boolean valGetSetImplemented = false; |
| 36 | + |
| 37 | + public Struct(GroupLayout layout, String pkg, Class<?> parent) { |
| 38 | + this.layout = layout; |
| 39 | + this.parent = parent; |
| 40 | + implementation.pkg = pkg; |
| 41 | + implementation.importImplicitly(new CType(parent)); |
| 42 | + implementation.accessSpecifier.pub = true; |
| 43 | + implementation.name = parent.getSimpleName() + "_J"; |
| 44 | + implementation.addField(SEGMENT_FIELD); |
| 45 | + implementation.addConstructor(genSetterConstructor()); |
| 46 | + implementation.addConstructor(genAllocatorConstructor(parent)); |
| 47 | + } |
| 48 | + |
| 49 | + public Struct(String pkg, Class<?> parent, Class<?> baseImpl) { |
| 50 | + this.layout = null; |
| 51 | + this.parent = parent; |
| 52 | + var baseWrapper = new CType(pkg + "." + baseImpl.getSimpleName() + "_J", baseImpl.getSimpleName() + "_J", false); |
| 53 | + implementation.pkg = pkg; |
| 54 | + implementation.accessSpecifier.pub = true; |
| 55 | + implementation.name = parent.getSimpleName() + "_J"; |
| 56 | + implementation.superclass = baseWrapper; |
| 57 | + implementation.addConstructor(SUPER_CALL_ASSIGN); |
| 58 | + implementation.addConstructor(SUPER_CALL_ALLOCATOR); |
| 59 | + } |
| 60 | + |
| 61 | + public boolean isBaseImplementation() { |
| 62 | + return this.layout != null; |
| 63 | + } |
| 64 | + |
| 65 | + private static CConstructor genSetterConstructor() { |
| 66 | + var constructor = new CConstructor(); |
| 67 | + constructor.accessSpecifier.pub = true; |
| 68 | + constructor.paramList.add(new CParameter(CType.MEMORY_SEGMENT, "segment")); |
| 69 | + constructor.code.append("this.segment = segment;"); |
| 70 | + return constructor; |
| 71 | + } |
| 72 | + |
| 73 | + private static CConstructor genAllocatorConstructor(Class<?> parent) { |
| 74 | + var constructor = new CConstructor(); |
| 75 | + constructor.accessSpecifier.pub = true; |
| 76 | + constructor.paramList.add(new CParameter(CType.SEGMENT_ALLOCATOR, "allocator")); |
| 77 | + constructor.code.append("this(").append(parent.getSimpleName()).append(".allocate(allocator));"); |
| 78 | + return constructor; |
| 79 | + } |
| 80 | + |
| 81 | + public boolean layoutEqualTo(MemoryLayout layout) { |
| 82 | + return layout.withName("").equals(this.layout.withName("")); |
| 83 | + } |
| 84 | + |
| 85 | + public void implementValueLayoutGetterSetters() { |
| 86 | + if (!isBaseImplementation()) return; |
| 87 | + if (valGetSetImplemented) return; |
| 88 | + valGetSetImplemented = true; |
| 89 | + layout.memberLayouts() |
| 90 | + .stream() |
| 91 | + .filter(ValueLayout.class::isInstance) |
| 92 | + .map(ValueLayout.class::cast) |
| 93 | + .flatMap((value) -> { |
| 94 | + @SuppressWarnings("OptionalGetWithoutIsPresent") //A name is always present, no need to check |
| 95 | + var name = value.name().get(); |
| 96 | + var getter = new CMethod(); |
| 97 | + var setter = new CMethod(); |
| 98 | + getter.accessSpecifier.pub = setter.accessSpecifier.pub = true; |
| 99 | + getter.name = setter.name = name; |
| 100 | + var oGetter = Arrays.stream(parent.getMethods()).filter((method) -> method.getParameterCount() == 1 && method.getName().equals(name + "$get")).findFirst(); |
| 101 | + if (oGetter.isEmpty()) { |
| 102 | + System.err.println("Could not generate getter/setter for struct field " + name); |
| 103 | + return Stream.empty(); |
| 104 | + } |
| 105 | + var type = new CType(oGetter.get().getReturnType()); |
| 106 | + setter.paramList.add(new CParameter(getter.returnType = type, "value")); |
| 107 | + getter.code.append("return ").append(parent.getSimpleName()).append('.').append(name).append("$get(segment);"); |
| 108 | + setter.code.append(parent.getSimpleName()).append('.').append(name).append("$set(segment, value);"); |
| 109 | + return Stream.of(getter, setter); |
| 110 | + }) |
| 111 | + .forEach(implementation::addMethod); |
| 112 | + } |
| 113 | + |
| 114 | + public void implementGroupLayoutGetters(List<Struct> allStructs) { |
| 115 | + if (!isBaseImplementation()) return; |
| 116 | + var structMapping = new HashMap<GroupLayout, Struct>(); |
| 117 | + allStructs.stream() |
| 118 | + .filter(Struct::isBaseImplementation) |
| 119 | + .filter((other) -> !(other.parent.isAssignableFrom(parent) || parent.isAssignableFrom(other.parent))) |
| 120 | + .forEach((other) -> layout.memberLayouts().stream() |
| 121 | + .filter(other::layoutEqualTo) |
| 122 | + .filter(GroupLayout.class::isInstance) |
| 123 | + .map(GroupLayout.class::cast) |
| 124 | + .forEach((subLayout) -> { |
| 125 | + if (subLayout.name().orElse("$anon$").startsWith("$anon$")) return; |
| 126 | + if (other.layoutEqualTo(subLayout)) { |
| 127 | + if (structMapping.containsKey(subLayout)) { |
| 128 | + var that = structMapping.get(subLayout); |
| 129 | + boolean thisTag = other.implementation.name.startsWith("tag"); |
| 130 | + boolean thatTag = that.implementation.name.startsWith("tag"); |
| 131 | + if (thisTag == thatTag) |
| 132 | + throw new IllegalStateException("Struct collision! " + structMapping.get(subLayout).parent + ", " + other.parent); |
| 133 | + else if (!thisTag) { |
| 134 | + structMapping.put(subLayout, other); |
| 135 | + } |
| 136 | + } |
| 137 | + structMapping.put(subLayout, other); |
| 138 | + } |
| 139 | + })); |
| 140 | + structMapping.forEach(this::setupGroupLayoutGetter); |
| 141 | + } |
| 142 | + |
| 143 | + private void setupGroupLayoutGetter(GroupLayout layout, Struct struct) { |
| 144 | + @SuppressWarnings("OptionalGetWithoutIsPresent") //A name is always present, no need to check |
| 145 | + var name = layout.name().get(); |
| 146 | + var cType = struct.implementation.asCType(); |
| 147 | + |
| 148 | + var field = new CField(); |
| 149 | + implementation.constructors.get(0).code.append("\n").append(name).append(" = new ").append(cType.simpleName()).append("(").append(parent.getSimpleName()).append(".").append(name).append("$slice(segment));"); |
| 150 | + field.accessSpecifier.pub = field.accessSpecifier.fin = true; |
| 151 | + field.name = name; |
| 152 | + field.type = cType; |
| 153 | + implementation.addField(field); |
| 154 | + } |
| 155 | + |
| 156 | + public void implementSequenceLayoutGetterSetter(List<Struct> allStructs) { |
| 157 | + |
| 158 | + } |
| 159 | +} |
0 commit comments