< prev index next >

src/jdk.jextract/share/classes/com/sun/tools/jextract/Utils.java

Print this page

        

@@ -25,19 +25,21 @@
 
 import java.foreign.layout.Function;
 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;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import javax.lang.model.SourceVersion;
 
 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;
 
 /**

@@ -182,10 +184,60 @@
             default:
                 throw new IllegalStateException("Unhandled cursor kind: " + c.kind());
         }
     }
 
+    // return builtin Record types accessible from the given Type
+    public static Stream<Cursor> getBuiltinRecordTypes(Type type) {
+        List<Cursor> recordTypes = new ArrayList<>();
+        fillBuiltinRecordTypes(type, recordTypes);
+        return recordTypes.stream().distinct();
+    }
+
+    private static void fillBuiltinRecordTypes(Type type, List<Cursor> 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<Path> findLibraryPath(Path[] paths, String libName) {
         return Arrays.stream(paths).
                 map(p -> p.resolve(System.mapLibraryName(libName))).
< prev index next >