Skip to content
Permalink
Browse files
8266682: sync jextract for LibraryLookup removal
Reviewed-by: mcimadamore, jvernee
  • Loading branch information
sundararajana committed May 7, 2021
1 parent 1198e1b commit 990758e3a69808abd451bd72b56604160d8899b4
@@ -27,7 +27,6 @@

import jdk.incubator.foreign.CLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.LibraryLookup;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemorySegment;
import jdk.incubator.foreign.ResourceScope;
@@ -49,12 +48,14 @@
//this is an hack - needed because clang_toggleCrashRecovery only takes effect _after_ the
//first call to createIndex.
try {
CLinker linker = CLinker.getInstance();
String putenv = IS_WINDOWS ? "_putenv" : "putenv";
MethodHandle PUT_ENV = linker.downcallHandle(LibraryLookup.ofDefault().lookup(putenv).get(),
Index_h.init();
if (!IS_WINDOWS) {
CLinker linker = CLinker.getInstance();
MethodHandle PUT_ENV = linker.downcallHandle(CLinker.findNative("putenv").get(),
MethodType.methodType(int.class, MemoryAddress.class),
FunctionDescriptor.of(CLinker.C_INT, CLinker.C_POINTER));
int res = (int) PUT_ENV.invokeExact(disableCrashRecovery.address());
int res = (int) PUT_ENV.invokeExact(disableCrashRecovery.address());
}
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}

Large diffs are not rendered by default.

@@ -170,47 +170,47 @@ public static MemoryAddress clang_index_getObjCPropertyDeclInfo ( Addressable x0
C_INT
);
static final MethodHandle clang_index_isEntityObjCContainerKind$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_isEntityObjCContainerKind",
"clang_index_isEntityObjCContainerKind",
"(I)I",
constants$0.clang_index_isEntityObjCContainerKind$FUNC, false
);
static final FunctionDescriptor clang_index_getObjCContainerDeclInfo$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getObjCContainerDeclInfo$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getObjCContainerDeclInfo",
"clang_index_getObjCContainerDeclInfo",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$0.clang_index_getObjCContainerDeclInfo$FUNC, false
);
static final FunctionDescriptor clang_index_getObjCInterfaceDeclInfo$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getObjCInterfaceDeclInfo$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getObjCInterfaceDeclInfo",
"clang_index_getObjCInterfaceDeclInfo",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$0.clang_index_getObjCInterfaceDeclInfo$FUNC, false
);
static final FunctionDescriptor clang_index_getObjCCategoryDeclInfo$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getObjCCategoryDeclInfo$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getObjCCategoryDeclInfo",
"clang_index_getObjCCategoryDeclInfo",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$0.clang_index_getObjCCategoryDeclInfo$FUNC, false
);
static final FunctionDescriptor clang_index_getObjCProtocolRefListInfo$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getObjCProtocolRefListInfo$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getObjCProtocolRefListInfo",
"clang_index_getObjCProtocolRefListInfo",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$0.clang_index_getObjCProtocolRefListInfo$FUNC, false
);
static final FunctionDescriptor clang_index_getObjCPropertyDeclInfo$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getObjCPropertyDeclInfo$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getObjCPropertyDeclInfo",
"clang_index_getObjCPropertyDeclInfo",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$0.clang_index_getObjCPropertyDeclInfo$FUNC, false
);
@@ -288,23 +288,23 @@ public static void clang_index_setClientEntity ( Addressable x0, Addressable x1
C_POINTER
);
static final MethodHandle clang_index_getIBOutletCollectionAttrInfo$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getIBOutletCollectionAttrInfo",
"clang_index_getIBOutletCollectionAttrInfo",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$1.clang_index_getIBOutletCollectionAttrInfo$FUNC, false
);
static final FunctionDescriptor clang_index_getCXXClassDeclInfo$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getCXXClassDeclInfo$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getCXXClassDeclInfo",
"clang_index_getCXXClassDeclInfo",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$1.clang_index_getCXXClassDeclInfo$FUNC, false
);
static final FunctionDescriptor clang_index_getClientContainer$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getClientContainer$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getClientContainer",
"clang_index_getClientContainer",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$1.clang_index_getClientContainer$FUNC, false
);
@@ -313,15 +313,15 @@ public static void clang_index_setClientEntity ( Addressable x0, Addressable x1
C_POINTER
);
static final MethodHandle clang_index_setClientContainer$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_setClientContainer",
"clang_index_setClientContainer",
"(Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;)V",
constants$1.clang_index_setClientContainer$FUNC, false
);
static final FunctionDescriptor clang_index_getClientEntity$FUNC = FunctionDescriptor.of(C_POINTER,
C_POINTER
);
static final MethodHandle clang_index_getClientEntity$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_getClientEntity",
"clang_index_getClientEntity",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$1.clang_index_getClientEntity$FUNC, false
);
@@ -330,7 +330,7 @@ public static void clang_index_setClientEntity ( Addressable x0, Addressable x1
C_POINTER
);
static final MethodHandle clang_index_setClientEntity$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_index_setClientEntity",
"clang_index_setClientEntity",
"(Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;)V",
constants$1.clang_index_setClientEntity$FUNC, false
);
@@ -426,15 +426,15 @@ public static void clang_indexLoc_getFileLocation ( MemorySegment loc, Addressa
C_POINTER
);
static final MethodHandle clang_IndexAction_create$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_IndexAction_create",
"clang_IndexAction_create",
"(Ljdk/incubator/foreign/MemoryAddress;)Ljdk/incubator/foreign/MemoryAddress;",
constants$2.clang_IndexAction_create$FUNC, false
);
static final FunctionDescriptor clang_IndexAction_dispose$FUNC = FunctionDescriptor.ofVoid(
C_POINTER
);
static final MethodHandle clang_IndexAction_dispose$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_IndexAction_dispose",
"clang_IndexAction_dispose",
"(Ljdk/incubator/foreign/MemoryAddress;)V",
constants$2.clang_IndexAction_dispose$FUNC, false
);
@@ -453,7 +453,7 @@ public static void clang_indexLoc_getFileLocation ( MemorySegment loc, Addressa
C_INT
);
static final MethodHandle clang_indexSourceFile$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_indexSourceFile",
"clang_indexSourceFile",
"(Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;IILjdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;ILjdk/incubator/foreign/MemoryAddress;ILjdk/incubator/foreign/MemoryAddress;I)I",
constants$2.clang_indexSourceFile$FUNC, false
);
@@ -472,7 +472,7 @@ public static void clang_indexLoc_getFileLocation ( MemorySegment loc, Addressa
C_INT
);
static final MethodHandle clang_indexSourceFileFullArgv$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_indexSourceFileFullArgv",
"clang_indexSourceFileFullArgv",
"(Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;IILjdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;ILjdk/incubator/foreign/MemoryAddress;ILjdk/incubator/foreign/MemoryAddress;I)I",
constants$2.clang_indexSourceFileFullArgv$FUNC, false
);
@@ -485,7 +485,7 @@ public static void clang_indexLoc_getFileLocation ( MemorySegment loc, Addressa
C_POINTER
);
static final MethodHandle clang_indexTranslationUnit$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_indexTranslationUnit",
"clang_indexTranslationUnit",
"(Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;IILjdk/incubator/foreign/MemoryAddress;)I",
constants$2.clang_indexTranslationUnit$FUNC, false
);
@@ -502,7 +502,7 @@ public static void clang_indexLoc_getFileLocation ( MemorySegment loc, Addressa
C_POINTER
);
static final MethodHandle clang_indexLoc_getFileLocation$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_indexLoc_getFileLocation",
"clang_indexLoc_getFileLocation",
"(Ljdk/incubator/foreign/MemorySegment;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;)V",
constants$2.clang_indexLoc_getFileLocation$FUNC, false
);
@@ -548,9 +548,11 @@ private static String libName() {
return "clang";
}

static final LibraryLookup[] LIBRARIES = RuntimeHelper.libraries(new String[] {
libName()
});
static {
RuntimeHelper.loadLibraries(new String[] { libName() });
}

public static void init() {}

public static class constants$3 {

@@ -566,7 +568,7 @@ private static String libName() {
)
);
static final MethodHandle clang_indexLoc_getCXSourceLocation$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_indexLoc_getCXSourceLocation",
"clang_indexLoc_getCXSourceLocation",
"(Ljdk/incubator/foreign/MemorySegment;)Ljdk/incubator/foreign/MemorySegment;",
constants$3.clang_indexLoc_getCXSourceLocation$FUNC, false
);
@@ -580,7 +582,7 @@ private static String libName() {
C_POINTER
);
static final MethodHandle clang_Type_visitFields$MH = RuntimeHelper.downcallHandle(
LIBRARIES, "clang_Type_visitFields",
"clang_Type_visitFields",
"(Ljdk/incubator/foreign/MemorySegment;Ljdk/incubator/foreign/MemoryAddress;Ljdk/incubator/foreign/MemoryAddress;)I",
constants$3.clang_Type_visitFields$FUNC, false
);
@@ -30,7 +30,6 @@
import jdk.incubator.foreign.Addressable;
import jdk.incubator.foreign.CLinker;
import jdk.incubator.foreign.FunctionDescriptor;
import jdk.incubator.foreign.LibraryLookup;
import jdk.incubator.foreign.MemoryAddress;
import jdk.incubator.foreign.MemoryLayout;
import jdk.incubator.foreign.MemorySegment;
@@ -63,28 +62,22 @@ private RuntimeHelper() {}

private final static SegmentAllocator THROWING_ALLOCATOR = (x, y) -> { throw new AssertionError("should not reach here"); };

static final LibraryLookup[] libraries(String... libNames) {
if (libNames.length == 0) {
return new LibraryLookup[] { LibraryLookup.ofDefault() };
} else {
return Arrays.stream(libNames)
.map(libName -> {
if (libName.indexOf(File.separatorChar) != -1) {
return LibraryLookup.ofPath(Path.of(libName));
} else {
return LibraryLookup.ofLibrary(libName);
}
})
.toArray(LibraryLookup[]::new);
static final void loadLibraries(String... libNames) {
for (String libName : libNames) {
if (libName.indexOf(File.separatorChar) != -1) {
System.load(libName);
} else {
System.loadLibrary(libName);
}
}
}

static final MemorySegment lookupGlobalVariable(LibraryLookup[] LIBRARIES, String name, MemoryLayout layout) {
return lookup(LIBRARIES, name).map(s -> s.address().asSegment(layout.byteSize(), ResourceScope.newImplicitScope())).orElse(null);
static final MemorySegment lookupGlobalVariable(String name, MemoryLayout layout) {
return lookup(name).map(s -> s.address().asSegment(layout.byteSize(), ResourceScope.newImplicitScope())).orElse(null);
}

static final MethodHandle downcallHandle(LibraryLookup[] LIBRARIES, String name, String desc, FunctionDescriptor fdesc, boolean variadic) {
return lookup(LIBRARIES, name).map(
static final MethodHandle downcallHandle(String name, String desc, FunctionDescriptor fdesc, boolean variadic) {
return lookup(name).map(
addr -> {
MethodType mt = MethodType.fromMethodDescriptorString(desc, LOADER);
return variadic ?
@@ -125,10 +118,8 @@ static MemorySegment asArray(MemoryAddress addr, MemoryLayout layout, int numEle
}

// Internals only below this point
private static final Optional<MemoryAddress> lookup(LibraryLookup[] LIBRARIES, String sym) {
return Stream.of(LIBRARIES)
.flatMap(l -> l.lookup(sym).stream())
.findFirst();
private static final Optional<MemoryAddress> lookup(String sym) {
return CLinker.findNative(sym);
}

private static class VarargsInvoker {
@@ -189,9 +189,7 @@ protected void emitImportSection() {
append("import java.lang.invoke.VarHandle;\n");
append("import java.nio.ByteOrder;\n");
append("import jdk.incubator.foreign.*;\n");
append("import static ");
append(OutputFactory.C_LANG_CONSTANTS_HOLDER);
append(".*;\n");
append("import static jdk.incubator.foreign.CLinker.*;\n");
}
}

@@ -195,7 +195,7 @@ private Constant emitMethodHandleField(String javaName, String nativeName, Funct
incrAlign();
indent();
if (!virtual) {
append(toplevel().headerClassName() + ".LIBRARIES, \"" + nativeName + "\"");
append("\"" + nativeName + "\"");
append(",\n");
indent();
}
@@ -391,8 +391,7 @@ private Constant emitSegmentField(String javaName, String nativeName, MemoryLayo
append("MemorySegment ");
append(fieldName);
append(" = ");
append("RuntimeHelper.lookupGlobalVariable(");
append(toplevel().headerClassName() + ".LIBRARIES, \"");
append("RuntimeHelper.lookupGlobalVariable(\"");
append(nativeName);
append("\", ");
append(layoutConstant.accessExpression());
@@ -33,6 +33,7 @@
import jdk.internal.jextract.impl.JavaSourceBuilder.FunctionInfo;

import javax.tools.JavaFileObject;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.constant.ClassDesc;
@@ -73,6 +74,7 @@
private final Set<Declaration.Typedef> unresolvedStructTypedefs = new HashSet<>();
private final Map<Type, String> functionTypeDefNames = new HashMap<>();
private final IncludeHelper includeHelper;
private final String[] libraryNames;

private void addStructDefinition(Declaration decl, String name) {
structClassNames.put(decl, name);
@@ -111,19 +113,19 @@ protected boolean functionSeen(Declaration.Function tree) {
public static JavaFileObject[] generateWrapped(Declaration.Scoped decl, String headerName,
String pkgName, IncludeHelper includeHelper, List<String> libraryNames) {
String clsName = Utils.javaSafeIdentifier(headerName.replace(".h", "_h"), true);
ToplevelBuilder toplevelBuilder = new ToplevelBuilder(pkgName, clsName, libraryNames.toArray(new String[0]));
return new OutputFactory(pkgName, toplevelBuilder, includeHelper).generate(decl);
ToplevelBuilder toplevelBuilder = new ToplevelBuilder(pkgName, clsName);
return new OutputFactory(pkgName, toplevelBuilder, includeHelper, libraryNames.toArray(new String[0])).generate(decl);
}

private OutputFactory(String pkgName, ToplevelBuilder toplevelBuilder, IncludeHelper includeHelper) {
private OutputFactory(String pkgName, ToplevelBuilder toplevelBuilder, IncludeHelper includeHelper,
String[] libraryNames) {
this.pkgName = pkgName;
this.toplevelBuilder = toplevelBuilder;
this.currentBuilder = toplevelBuilder;
this.includeHelper = includeHelper;
this.libraryNames = libraryNames;
}

static final String C_LANG_CONSTANTS_HOLDER = "jdk.incubator.foreign.CLinker";

JavaFileObject[] generate(Declaration.Scoped decl) {
//generate all decls
decl.members().forEach(this::generateDecl);
@@ -148,7 +150,28 @@ private String getRuntimeHelperSource() throws URISyntaxException, IOException {
URL runtimeHelper = OutputFactory.class.getResource("resources/RuntimeHelper.java.template");
return (pkgName.isEmpty()? "" : "package " + pkgName + ";\n") +
String.join("\n", Files.readAllLines(Paths.get(runtimeHelper.toURI())))
.replace("${C_LANG}", C_LANG_CONSTANTS_HOLDER);
.replace("${LOADLIBRARIES}", generateLoadLibraries());
}

private String generateLoadLibraries() {
StringBuilder buf = new StringBuilder();
for (String lib : libraryNames) {
String quotedName = quoteLibraryName(lib);
if (lib.indexOf(File.separatorChar) != -1) {
buf.append("System.load(\"");
buf.append(quotedName);
buf.append("\");\n");
} else {
buf.append("System.loadLibrary(\"");
buf.append(quotedName);
buf.append("\");\n");
}
}
return buf.toString();
}

private String quoteLibraryName(String lib) {
return lib.replace("\\", "\\\\"); // double up slashes
}

private void generateDecl(Declaration tree) {

0 comments on commit 990758e

Please sign in to comment.