1 /*
   2  * Copyright (c) 2019, 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 package com.sun.tools.jextract;
  24 
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.util.ArrayList;
  28 import java.util.HashMap;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.Set;
  32 import java.util.logging.Level;
  33 
  34 import com.sun.tools.jextract.parser.MacroParser;
  35 import com.sun.tools.jextract.tree.Tree;
  36 import com.sun.tools.jextract.tree.EnumTree;
  37 import com.sun.tools.jextract.tree.FieldTree;
  38 import com.sun.tools.jextract.tree.FunctionTree;
  39 import com.sun.tools.jextract.tree.MacroTree;
  40 import com.sun.tools.jextract.tree.VarTree;
  41 
  42 /**
  43  * This extended factory generates a class with only static methods and fields. A native
  44  * library interface instance (of the given header file) is kept as a static private field.
  45  * One static method is generated for every library interface method. Enum and macro constants
  46  * are mapped to static final fields. By importing the "static forwarder" class, the user code
  47  * looks more or less like C code. Libraries.bind and header interface usage is hidden.
  48  */
  49 final class JavaSourceFactoryExt extends JavaSourceFactory {
  50     // field name for the header interface instance.
  51     private static final String STATICS_LIBRARY_FIELD_NAME = "_theLibrary";
  52     private final JavaSourceBuilderExt header_jsb;
  53     private final List<JavaSourceBuilderExt> enums;
  54 
  55     JavaSourceFactoryExt(Context ctx, HeaderFile header) {
  56         super(ctx, header);
  57         log.print(Level.INFO, () -> "Instantiate StaticForwarderGenerator for " + header.path);
  58         header_jsb = new JavaSourceBuilderExt();
  59         enums = new ArrayList<>();
  60     }
  61 
  62     @Override
  63     public Map<String, String> generate(List<Tree> decls) {
  64         header_jsb.addPackagePrefix(headerFile.pkgName);
  65         String ifaceClsName = headerFile.headerClsName;
  66         String forwarderName = headerFile.staticForwarderClsName;
  67         header_jsb.classBegin(forwarderName, false);
  68 
  69         header_jsb.addLibraryField(ifaceClsName, STATICS_LIBRARY_FIELD_NAME);
  70         header_jsb.emitScopeAccessor(STATICS_LIBRARY_FIELD_NAME);
  71 
  72         Map<String, String> srcMap = super.generate(decls);
  73         enums.forEach(header_jsb::addNestedType);
  74 
  75         header_jsb.classEnd();
  76         String src = header_jsb.build();
  77         if (srcDir != null) {
  78             try {
  79                 Path srcPath = srcDir.resolve(forwarderName + ".java");
  80                 Files.write(srcPath, List.of(src));
  81             } catch (Exception ex) {
  82                 handleException(ex);
  83             }
  84         }
  85         srcMap.put(headerFile.pkgName + "." + forwarderName, src);
  86         return srcMap;
  87     }
  88 
  89     @Override
  90     public Boolean visitVar(VarTree varTree, JType jt) {
  91         if (super.visitVar(varTree, jt)) {
  92             String fieldName = varTree.name();
  93             assert !fieldName.isEmpty();
  94             header_jsb.emitStaticForwarder(varTree, jt, STATICS_LIBRARY_FIELD_NAME);
  95             return true;
  96         } else {
  97             return false;
  98         }
  99     }
 100 
 101     @Override
 102     public Boolean visitEnum(EnumTree enumTree, JType jt) {
 103         if (super.visitEnum(enumTree, jt)) {
 104             if (enumTree.name().isEmpty()) {
 105                 enumTree.constants().forEach(constant -> addConstant(header_jsb, constant.name(),
 106                     headerFile.dictionary().lookup(constant.type()),
 107                     constant.enumConstant().get()));
 108             } else {
 109                 JavaSourceBuilderExt enumBuilder = new JavaSourceBuilderExt(header_jsb.align() + 1);
 110                 enumBuilder.interfaceBegin(enumTree.name(), true);
 111                 enumTree.constants().forEach(constant -> addConstant(enumBuilder, constant.name(),
 112                     headerFile.dictionary().lookup(constant.type()),
 113                     constant.enumConstant().get()));
 114                 enumBuilder.interfaceEnd();
 115                 enums.add(enumBuilder);
 116             }
 117             return true;
 118         } else {
 119             return false;
 120         }
 121     }
 122 
 123     @Override
 124     public Boolean visitFunction(FunctionTree funcTree, JType jt) {
 125         if (super.visitFunction(funcTree, jt)) {
 126             header_jsb.emitStaticForwarder(funcTree, (JType.Function)jt, STATICS_LIBRARY_FIELD_NAME);
 127             return true;
 128         } else {
 129             return false;
 130         }
 131     }
 132 
 133     @Override
 134     public Boolean visitMacro(MacroTree macroTree, JType jt) {
 135         if (super.visitMacro(macroTree, jt)) {
 136             String name = macroTree.name();
 137             MacroParser.Macro macro = macroTree.macro().get();
 138             log.print(Level.FINE, () -> "Adding macro " + name);
 139             addConstant(header_jsb, Utils.toMacroName(name), macro.type(), macro.value());
 140             return true;
 141         } else {
 142             return false;
 143         }
 144     }
 145 
 146     private void addConstant(JavaSourceBuilderExt jsb, String name, JType type, Object value) {
 147         Object constantValue = makeConstantValue(type, value);
 148         jsb.addField(name, type, constantValue, STATICS_LIBRARY_FIELD_NAME);
 149     }
 150 
 151     private Object makeConstantValue(JType type, Object value) {
 152         switch (type.getDescriptor()) {
 153             case "Z":
 154                 return ((long)value) != 0;
 155             case "C":
 156                 return (char)(long)value;
 157             case "B":
 158                 return (byte)(long)value;
 159             case "S":
 160                 return (short)(long)value;
 161             case "I":
 162                 return (int)(long)value;
 163             case "F":
 164                 return (float)(double)value;
 165             case "J": case "D":
 166                 return value;
 167             default:
 168                 return null;
 169         }
 170     }
 171 }