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