1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  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)) {
  59             throw new IllegalArgumentException("Invalid start character for an identifier: " + ch);
  60         }
  61 
  62         for (int i = 1; i < length; i++) {
  63             ch = name.codePointAt(i);
  64             if (!Character.isJavaIdentifierPart(ch)) {
  65                 throw new IllegalArgumentException("Invalid character for an identifier: " + ch);
  66             }
  67         }
  68     }
  69 
  70     public static void validPackageName(String name) {
  71         if (name.isEmpty()) {
  72             throw new IllegalArgumentException();
  73         }
  74         int idx = name.lastIndexOf('.');
  75         if (idx == -1) {
  76            validSimpleIdentifier(name);
  77         } else {
  78             validSimpleIdentifier(name.substring(idx + 1));
  79             validPackageName(name.substring(0, idx));
  80         }
  81     }
  82 
  83     public static String toJavaIdentifier(String str) {
  84         final int size = str.length();
  85         StringBuilder sb = new StringBuilder(size);
  86         if (! Character.isJavaIdentifierStart(str.charAt(0))) {
  87             sb.append('_');
  88         }
  89         for (int i = 0; i < size; i++) {
  90             char ch = str.charAt(i);
  91             if (Character.isJavaIdentifierPart(ch)) {
  92                 sb.append(ch);
  93             } else {
  94                 sb.append('_');
  95             }
  96         }
  97         return sb.toString();
  98     }
  99 
 100     public static String toClassName(String cname) {
 101         StringBuilder sb = new StringBuilder(cname.length());
 102         cname = toJavaIdentifier(cname);
 103         sb.append(cname);
 104         if (SourceVersion.isKeyword(cname)) {
 105             sb.append("$");
 106         }
 107         return sb.toString();
 108     }
 109 
 110     public static String toInternalName(String pkg, String name, String... nested) {
 111         if ((pkg == null || pkg.isEmpty()) && nested == null) {
 112             return name;
 113         }
 114 
 115         StringBuilder sb = new StringBuilder();
 116         if (pkg != null && ! pkg.isEmpty()) {
 117             sb.append(pkg.replace('.', '/'));
 118             if (sb.charAt(sb.length() - 1) != '/') {
 119                 sb.append('/');
 120             }
 121         }
 122         sb.append(name);
 123         for (String n: nested) {
 124             sb.append('$');
 125             sb.append(n);
 126         }
 127         return sb.toString();
 128     }
 129 
 130     public static String getName(Type type) {
 131         return LayoutUtils.getName(type);
 132     }
 133 
 134     public static Function getFunction(Type type) {
 135         return LayoutUtils.getFunction(type);
 136     }
 137 
 138     public static Class<?> unboxIfNeeded(Class<?> clazz) {
 139         if (clazz == Boolean.class) {
 140             return boolean.class;
 141         } else if (clazz == Void.class) {
 142             return void.class;
 143         } else if (clazz == Byte.class) {
 144             return byte.class;
 145         } else if (clazz == Character.class) {
 146             return char.class;
 147         } else if (clazz == Short.class) {
 148             return short.class;
 149         } else if (clazz == Integer.class) {
 150             return int.class;
 151         } else if (clazz == Long.class) {
 152             return long.class;
 153         } else if (clazz == Float.class) {
 154             return float.class;
 155         } else if (clazz == Double.class) {
 156             return double.class;
 157         } else {
 158             return clazz;
 159         }
 160     }
 161 
 162     public static Stream<Cursor> flattenableChildren(Cursor c) {
 163         return c.children()
 164                 .filter(cx -> cx.isAnonymousStruct() || cx.kind() == CursorKind.FieldDecl);
 165     }
 166 
 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 }