< prev index next >

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

Print this page




  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package com.sun.tools.jextract;
  25 
  26 import java.foreign.layout.Function;
  27 import java.foreign.layout.Layout;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;

  30 import java.util.Arrays;
  31 import java.util.List;
  32 import java.util.Optional;
  33 import java.util.stream.Collectors;
  34 import java.util.stream.Stream;
  35 import javax.lang.model.SourceVersion;
  36 
  37 import jdk.internal.clang.Cursor;
  38 import jdk.internal.clang.CursorKind;

  39 import jdk.internal.clang.Type;
  40 import com.sun.tools.jextract.tree.LayoutUtils;
  41 import jdk.internal.clang.TypeKind;
  42 
  43 /**
  44  * General utility functions
  45  */
  46 public class Utils {
  47     public static void validSimpleIdentifier(String name) {
  48         int length = name.length();
  49         if (length == 0) {
  50             throw new IllegalArgumentException();
  51         }
  52 
  53         int ch = name.codePointAt(0);
  54         if (length == 1 && ch == '_') {
  55             throw new IllegalArgumentException("'_' is no longer valid identifier.");
  56         }
  57 
  58         if (!Character.isJavaIdentifierStart(ch)) {


 167     public static Optional<Cursor> lastChild(Cursor c) {
 168         List<Cursor> children = flattenableChildren(c)
 169                 .collect(Collectors.toList());
 170         return children.isEmpty() ? Optional.empty() : Optional.of(children.get(children.size() - 1));
 171     }
 172 
 173     public static boolean hasIncompleteArray(Cursor c) {
 174         switch (c.kind()) {
 175             case FieldDecl:
 176                 return c.type().kind() == TypeKind.IncompleteArray;
 177             case UnionDecl:
 178                 return flattenableChildren(c)
 179                         .anyMatch(Utils::hasIncompleteArray);
 180             case StructDecl:
 181                 return lastChild(c).map(Utils::hasIncompleteArray).orElse(false);
 182             default:
 183                 throw new IllegalStateException("Unhandled cursor kind: " + c.kind());
 184         }
 185     }
 186 


















































 187     // return the absolute path of the library of given name by searching
 188     // in the given array of paths.
 189     public static Optional<Path> findLibraryPath(Path[] paths, String libName) {
 190         return Arrays.stream(paths).
 191                 map(p -> p.resolve(System.mapLibraryName(libName))).
 192                 filter(Files::isRegularFile).map(Path::toAbsolutePath).findFirst();
 193     }
 194 }


  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package com.sun.tools.jextract;
  25 
  26 import java.foreign.layout.Function;
  27 import java.foreign.layout.Layout;
  28 import java.nio.file.Files;
  29 import java.nio.file.Path;
  30 import java.util.ArrayList;
  31 import java.util.Arrays;
  32 import java.util.List;
  33 import java.util.Optional;
  34 import java.util.stream.Collectors;
  35 import java.util.stream.Stream;
  36 import javax.lang.model.SourceVersion;
  37 
  38 import jdk.internal.clang.Cursor;
  39 import jdk.internal.clang.CursorKind;
  40 import jdk.internal.clang.SourceLocation;
  41 import jdk.internal.clang.Type;
  42 import com.sun.tools.jextract.tree.LayoutUtils;
  43 import jdk.internal.clang.TypeKind;
  44 
  45 /**
  46  * General utility functions
  47  */
  48 public class Utils {
  49     public static void validSimpleIdentifier(String name) {
  50         int length = name.length();
  51         if (length == 0) {
  52             throw new IllegalArgumentException();
  53         }
  54 
  55         int ch = name.codePointAt(0);
  56         if (length == 1 && ch == '_') {
  57             throw new IllegalArgumentException("'_' is no longer valid identifier.");
  58         }
  59 
  60         if (!Character.isJavaIdentifierStart(ch)) {


 169     public static Optional<Cursor> lastChild(Cursor c) {
 170         List<Cursor> children = flattenableChildren(c)
 171                 .collect(Collectors.toList());
 172         return children.isEmpty() ? Optional.empty() : Optional.of(children.get(children.size() - 1));
 173     }
 174 
 175     public static boolean hasIncompleteArray(Cursor c) {
 176         switch (c.kind()) {
 177             case FieldDecl:
 178                 return c.type().kind() == TypeKind.IncompleteArray;
 179             case UnionDecl:
 180                 return flattenableChildren(c)
 181                         .anyMatch(Utils::hasIncompleteArray);
 182             case StructDecl:
 183                 return lastChild(c).map(Utils::hasIncompleteArray).orElse(false);
 184             default:
 185                 throw new IllegalStateException("Unhandled cursor kind: " + c.kind());
 186         }
 187     }
 188 
 189     // return builtin Record types accessible from the given Type
 190     public static Stream<Cursor> getBuiltinRecordTypes(Type type) {
 191         List<Cursor> recordTypes = new ArrayList<>();
 192         fillBuiltinRecordTypes(type, recordTypes);
 193         return recordTypes.stream().distinct();
 194     }
 195 
 196     private static void fillBuiltinRecordTypes(Type type, List<Cursor> recordTypes) {
 197         switch (type.kind()) {
 198             case ConstantArray:
 199             case IncompleteArray:
 200                 fillBuiltinRecordTypes(type.getElementType(), recordTypes);
 201                 break;
 202 
 203             case FunctionProto:
 204             case FunctionNoProto: {
 205                 final int numArgs = type.numberOfArgs();
 206                 for (int i = 0; i < numArgs; i++) {
 207                     fillBuiltinRecordTypes(type.argType(i), recordTypes);
 208                 }
 209                 fillBuiltinRecordTypes(type.resultType(), recordTypes);
 210             }
 211             break;
 212 
 213             case Record: {
 214                 Cursor c = type.getDeclarationCursor();
 215                 if (c.isDefinition()) {
 216                     SourceLocation sloc = c.getSourceLocation();
 217                     if (sloc != null && sloc.getFileLocation().path() == null) {
 218                         recordTypes.add(c);
 219                     }
 220                 }
 221             }
 222             break;
 223 
 224             case BlockPointer:
 225             case Pointer:
 226                 fillBuiltinRecordTypes(type.getPointeeType().canonicalType(), recordTypes);
 227                 break;
 228 
 229             case Unexposed:
 230             case Elaborated:
 231             case Typedef:
 232                 fillBuiltinRecordTypes(type.canonicalType(), recordTypes);
 233                 break;
 234 
 235             default: // nothing to do
 236         }
 237     }
 238 
 239     // return the absolute path of the library of given name by searching
 240     // in the given array of paths.
 241     public static Optional<Path> findLibraryPath(Path[] paths, String libName) {
 242         return Arrays.stream(paths).
 243                 map(p -> p.resolve(System.mapLibraryName(libName))).
 244                 filter(Files::isRegularFile).map(Path::toAbsolutePath).findFirst();
 245     }
 246 }
< prev index next >