--- old/src/jdk.jextract/share/classes/com/sun/tools/jextract/Utils.java 2019-04-08 17:27:37.000000000 +0530 +++ new/src/jdk.jextract/share/classes/com/sun/tools/jextract/Utils.java 2019-04-08 17:27:34.000000000 +0530 @@ -27,6 +27,7 @@ import java.foreign.layout.Layout; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -36,6 +37,7 @@ import jdk.internal.clang.Cursor; import jdk.internal.clang.CursorKind; +import jdk.internal.clang.SourceLocation; import jdk.internal.clang.Type; import com.sun.tools.jextract.tree.LayoutUtils; import jdk.internal.clang.TypeKind; @@ -184,6 +186,56 @@ } } + // return builtin Record types accessible from the given Type + public static Stream getBuiltinRecordTypes(Type type) { + List recordTypes = new ArrayList<>(); + fillBuiltinRecordTypes(type, recordTypes); + return recordTypes.stream().distinct(); + } + + private static void fillBuiltinRecordTypes(Type type, List recordTypes) { + switch (type.kind()) { + case ConstantArray: + case IncompleteArray: + fillBuiltinRecordTypes(type.getElementType().canonicalType(), recordTypes); + break; + + case FunctionProto: + case FunctionNoProto: { + final int numArgs = type.numberOfArgs(); + for (int i = 0; i < numArgs; i++) { + fillBuiltinRecordTypes(type.argType(i), recordTypes); + } + fillBuiltinRecordTypes(type.resultType(), recordTypes); + } + break; + + case Record: { + Cursor c = type.getDeclarationCursor(); + if (c.isDefinition()) { + SourceLocation sloc = c.getSourceLocation(); + if (sloc != null && sloc.getFileLocation().path() == null) { + recordTypes.add(c); + } + } + } + break; + + case BlockPointer: + case Pointer: + fillBuiltinRecordTypes(type.getPointeeType().canonicalType(), recordTypes); + break; + + case Unexposed: + case Elaborated: + case Typedef: + fillBuiltinRecordTypes(type.canonicalType(), recordTypes); + break; + + default: // nothing to do + } + } + // return the absolute path of the library of given name by searching // in the given array of paths. public static Optional findLibraryPath(Path[] paths, String libName) {