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             FunctionDescriptor descriptor,
 51             boolean isVarargs,
 52             Optional<List<String>> parameterNames) {
 53 
 54         static FunctionInfo ofFunction(MethodType methodType, FunctionDescriptor functionDescriptor, boolean isVarargs, List<String> parameterNames) {
 55             return new FunctionInfo(methodType, functionDescriptor, isVarargs, Optional.of(parameterNames));
 56         }
 57 
 58         static FunctionInfo ofFunctionPointer(MethodType methodType, FunctionDescriptor functionDescriptor) {
 59             return new FunctionInfo(methodType, functionDescriptor, false, Optional.empty());
 60         }
 61     }
 62 
 63     public static record VarInfo(
 64             Class<?> carrier,
 65             MemoryLayout layout,
 66             Optional<String> fiName) {
 67 
 68         static VarInfo ofVar(Class<?> carrier, MemoryLayout layout) {
 69             return new VarInfo(carrier, layout, Optional.empty());
 70         }
 71 
 72         static VarInfo ofFunctionalPointerVar(Class<?> carrier, MemoryLayout layout, String fiName) {
 73             return new VarInfo(carrier, layout, Optional.ofNullable(fiName));
 74         }
 75     }
 76 
 77     public void addVar(String javaName, String nativeName, ClassSourceBuilder.VarInfo varInfo) {
 78         throw new UnsupportedOperationException();
 79     }
 80 
 81     public void addFunction(String javaName, String nativeName, ClassSourceBuilder.FunctionInfo functionInfo) {
 82         throw new UnsupportedOperationException();
 83     }
 84 
 85     public void addConstant(String javaName, Class<?> type, Object value) {
 86         throw new UnsupportedOperationException();
 87     }
 88 
 89     public void addTypedef(String name, String superClass, Type type) {
 90         throw new UnsupportedOperationException();
 91     }
 92 
 93     public StructBuilder addStruct(String name, Declaration parent, GroupLayout layout, Type type) {
 94         throw new UnsupportedOperationException();
 95     }
 96 
 97     public String addFunctionalInterface(String name, ClassSourceBuilder.FunctionInfo fInfo) {
 98         throw new UnsupportedOperationException();
 99     }
100 
101     abstract public List<JavaFileObject> toFiles();
102 
103     public abstract String packageName();
104 
105     Set<String> nestedClassNames = new HashSet<>();
106     int nestedClassNameCount = 0;
107 
108     /*
109      * We may have case-insensitive name collision! A C program may have
110      * defined structs/unions/typedefs with the names FooS, fooS, FoOs, fOOs.
111      * Because we map structs/unions/typedefs to nested classes of header classes,
112      * such a case-insensitive name collision is problematic. This is because in
113      * a case-insensitive file system javac will overwrite classes for
114      * Header$CFooS, Header$CfooS, Header$CFoOs and so on! We solve this by
115      * generating unique case-insensitive names for nested classes.
116      */
117     final String uniqueNestedClassName(String name) {
118         name = Utils.javaSafeIdentifier(name);
119         var notSeen = nestedClassNames.add(name.toLowerCase());
120         var notEnclosed = !isEnclosedBySameName(name.toLowerCase());
121         return notSeen && notEnclosed? name : (name + "$" + nestedClassNameCount++);
122     }
123 
124     abstract boolean isEnclosedBySameName(String name);
125 
126     abstract protected void emitWithConstantClass(Consumer<ConstantBuilder> constantConsumer);
127 }