1 /*
  2  * Copyright (c) 2021, 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.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package jdk.internal.jextract.impl;
 27 
 28 import jdk.incubator.foreign.FunctionDescriptor;
 29 import jdk.incubator.foreign.GroupLayout;
 30 import jdk.incubator.foreign.MemoryLayout;
 31 import jdk.incubator.jextract.Declaration;
 32 import jdk.incubator.jextract.Type;
 33 
 34 import javax.tools.JavaFileObject;
 35 import java.lang.invoke.MethodType;
 36 import java.util.ArrayList;
 37 import java.util.HashSet;
 38 import java.util.List;
 39 import java.util.Optional;
 40 import java.util.Set;
 41 import java.util.function.Consumer;
 42 import java.util.stream.Collectors;
 43 
 44 public abstract class JavaSourceBuilder {
 45 
 46     // public API (used by OutputFactory)
 47 
 48     public static record FunctionInfo(
 49             MethodType methodType,
 50             MethodType reverseMethodType,
 51             FunctionDescriptor descriptor,
 52             boolean isVarargs,
 53             Optional<List<String>> parameterNames) {
 54 
 55         static FunctionInfo ofFunction(MethodType methodType, FunctionDescriptor functionDescriptor, boolean isVarargs, List<String> parameterNames) {
 56             return new FunctionInfo(methodType, null, functionDescriptor, isVarargs, Optional.of(parameterNames));
 57         }
 58 
 59         static FunctionInfo ofFunctionPointer(MethodType upcallType, MethodType downcallType, FunctionDescriptor functionDescriptor,
 60                  Optional<List<String>> parameterNames) {
 61             return new FunctionInfo(upcallType, downcallType, functionDescriptor, false, parameterNames);
 62         }
 63     }
 64 
 65     public static record VarInfo(
 66             Class<?> carrier,
 67             MemoryLayout layout,
 68             Optional<String> fiName) {
 69 
 70         static VarInfo ofVar(Class<?> carrier, MemoryLayout layout) {
 71             return new VarInfo(carrier, layout, Optional.empty());
 72         }
 73 
 74         static VarInfo ofFunctionalPointerVar(Class<?> carrier, MemoryLayout layout, String fiName) {
 75             return new VarInfo(carrier, layout, Optional.ofNullable(fiName));
 76         }
 77     }
 78 
 79     public void addVar(String javaName, String nativeName, ClassSourceBuilder.VarInfo varInfo) {
 80         throw new UnsupportedOperationException();
 81     }
 82 
 83     public void addFunction(String javaName, String nativeName, ClassSourceBuilder.FunctionInfo functionInfo) {
 84         throw new UnsupportedOperationException();
 85     }
 86 
 87     public void addConstant(String javaName, Class<?> type, Object value) {
 88         throw new UnsupportedOperationException();
 89     }
 90 
 91     public void addTypedef(String name, String superClass, Type type) {
 92         throw new UnsupportedOperationException();
 93     }
 94 
 95     public StructBuilder addStruct(String name, Declaration parent, GroupLayout layout, Type type) {
 96         throw new UnsupportedOperationException();
 97     }
 98 
 99     public String addFunctionalInterface(String name, ClassSourceBuilder.FunctionInfo fInfo) {
100         throw new UnsupportedOperationException();
101     }
102 
103     abstract public List<JavaFileObject> toFiles();
104 
105     public abstract String packageName();
106 
107     Set<String> nestedClassNames = new HashSet<>();
108     int nestedClassNameCount = 0;
109 
110     /*
111      * We may have case-insensitive name collision! A C program may have
112      * defined structs/unions/typedefs with the names FooS, fooS, FoOs, fOOs.
113      * Because we map structs/unions/typedefs to nested classes of header classes,
114      * such a case-insensitive name collision is problematic. This is because in
115      * a case-insensitive file system javac will overwrite classes for
116      * Header$CFooS, Header$CfooS, Header$CFoOs and so on! We solve this by
117      * generating unique case-insensitive names for nested classes.
118      */
119     final String uniqueNestedClassName(String name) {
120         name = Utils.javaSafeIdentifier(name);
121         var notSeen = nestedClassNames.add(name.toLowerCase());
122         var notEnclosed = !isEnclosedBySameName(name.toLowerCase());
123         return notSeen && notEnclosed? name : (name + "$" + nestedClassNameCount++);
124     }
125 
126     abstract boolean isEnclosedBySameName(String name);
127 
128     abstract protected void emitWithConstantClass(Consumer<ConstantBuilder> constantConsumer);
129 }