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.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)) {
  61             throw new IllegalArgumentException("Invalid start character for an identifier: " + ch);
  62         }
  63 
  64         for (int i = 1; i < length; i++) {
  65             ch = name.codePointAt(i);
  66             if (!Character.isJavaIdentifierPart(ch)) {
  67                 throw new IllegalArgumentException("Invalid character for an identifier: " + ch);
  68             }
  69         }
  70     }
  71 
  72     public static void validPackageName(String name) {
  73         if (name.isEmpty()) {
  74             throw new IllegalArgumentException();
  75         }
  76         int idx = name.lastIndexOf('.');
  77         if (idx == -1) {
  78            validSimpleIdentifier(name);
  79         } else {
  80             validSimpleIdentifier(name.substring(idx + 1));
  81             validPackageName(name.substring(0, idx));
  82         }
  83     }
  84 
  85     public static String toJavaIdentifier(String str) {
  86         final int size = str.length();
  87         StringBuilder sb = new StringBuilder(size);
  88         if (! Character.isJavaIdentifierStart(str.charAt(0))) {
  89             sb.append('_');
  90         }
  91         for (int i = 0; i < size; i++) {
  92             char ch = str.charAt(i);
  93             if (Character.isJavaIdentifierPart(ch)) {
  94                 sb.append(ch);
  95             } else {
  96                 sb.append('_');
  97             }
  98         }
  99         return sb.toString();
 100     }
 101 
 102     public static String toClassName(String cname) {
 103         StringBuilder sb = new StringBuilder(cname.length());
 104         cname = toJavaIdentifier(cname);
 105         sb.append(cname);
 106         if (SourceVersion.isKeyword(cname)) {
 107             sb.append("$");
 108         }
 109         return sb.toString();
 110     }
 111 
 112     public static String toInternalName(String pkg, String name, String... nested) {
 113         if ((pkg == null || pkg.isEmpty()) && nested == null) {
 114             return name;
 115         }
 116 
 117         StringBuilder sb = new StringBuilder();
 118         if (pkg != null && ! pkg.isEmpty()) {
 119             sb.append(pkg.replace('.', '/'));
 120             if (sb.charAt(sb.length() - 1) != '/') {
 121                 sb.append('/');
 122             }
 123         }
 124         sb.append(name);
 125         for (String n: nested) {
 126             sb.append('$');
 127             sb.append(n);
 128         }
 129         return sb.toString();
 130     }
 131 
 132     public static String getName(Type type) {
 133         return LayoutUtils.getName(type);
 134     }
 135 
 136     public static Function getFunction(Type type) {
 137         return LayoutUtils.getFunction(type);
 138     }
 139 
 140     public static Class<?> unboxIfNeeded(Class<?> clazz) {
 141         if (clazz == Boolean.class) {
 142             return boolean.class;
 143         } else if (clazz == Void.class) {
 144             return void.class;
 145         } else if (clazz == Byte.class) {
 146             return byte.class;
 147         } else if (clazz == Character.class) {
 148             return char.class;
 149         } else if (clazz == Short.class) {
 150             return short.class;
 151         } else if (clazz == Integer.class) {
 152             return int.class;
 153         } else if (clazz == Long.class) {
 154             return long.class;
 155         } else if (clazz == Float.class) {
 156             return float.class;
 157         } else if (clazz == Double.class) {
 158             return double.class;
 159         } else {
 160             return clazz;
 161         }
 162     }
 163 
 164     public static Stream<Cursor> flattenableChildren(Cursor c) {
 165         return c.children()
 166                 .filter(cx -> cx.isAnonymousStruct() || cx.kind() == CursorKind.FieldDecl);
 167     }
 168 
 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().canonicalType(), 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 }