1 /* 2 * Copyright (c) 2015, 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 jdk.internal.clang.Cursor; 26 import jdk.internal.clang.CursorKind; 27 import jdk.internal.clang.Type; 28 import jdk.internal.clang.TypeKind; 29 30 import java.nio.file.Path; 31 import java.util.concurrent.atomic.AtomicInteger; 32 import java.util.logging.Logger; 33 import java.util.List; 34 35 /** 36 * This class represent a native code header file 37 */ 38 public final class HeaderFile { 39 final Path path; 40 final String pkgName; 41 final String clsName; 42 final TypeDictionary dict; 43 // The top header file cause this file to be parsed 44 HeaderFile main; 45 CodeFactory cf; 46 List<String> libraries; 47 List<String> libraryPaths; 48 49 private final AtomicInteger serialNo; 50 final Logger logger = Logger.getLogger(getClass().getPackage().getName()); 51 52 HeaderFile(Path path, String pkgName, String clsName, HeaderFile main) { 53 this.path = path; 54 this.pkgName = pkgName; 55 this.clsName = clsName; 56 dict = TypeDictionary.of(pkgName); 57 serialNo = new AtomicInteger(); 58 this.main = main == null ? this : main; 59 } 60 61 void useLibraries(List<String> libraries, List<String> libraryPaths) { 62 this.libraries = libraries; 63 this.libraryPaths = libraryPaths; 64 } 65 66 /** 67 * Call this function to enable code generation for this HeaderFile. 68 * This function should only be called once to turn on code generation and before process any cursor. 69 * @param cf The CodeFactory used to generate code 70 */ 71 void useCodeFactory(CodeFactory cf) { 72 if (null != this.cf) { 73 logger.config(() -> "CodeFactory had been initialized for " + path); 74 // Diagnosis code 75 if (Main.DEBUG) { 76 new Throwable().printStackTrace(System.err); 77 } 78 } else { 79 this.cf = cf; 80 } 81 } 82 83 @Override 84 public String toString() { 85 return "HeaderFile(path=" + path + ")"; 86 } 87 88 private int serialNo() { 89 return serialNo.incrementAndGet(); 90 } 91 92 void processCursor(Cursor c, HeaderFile main, boolean isBuiltIn) { 93 if (c.isDeclaration()) { 94 Type t = c.type(); 95 JType jt = dict.computeIfAbsent(t, type -> { 96 logger.fine(() -> "PH: Compute type for " + type.spelling()); 97 return define(type); 98 }); 99 assert (jt instanceof JType2); 100 // Only main file can define interface 101 if (cf != null && this.main == main) { 102 cf.addType(jt, c); 103 } 104 } else if (c.isPreprocessing()) { 105 if (cf != null && c.kind() == CursorKind.MacroDefinition && !isBuiltIn && this.main == main) { 106 cf.addMacro(c); 107 } 108 } 109 } 110 111 JType globalLookup(Type type) { 112 JType jt; 113 try { 114 jt = dict.lookup(type); 115 if (null == jt) { 116 jt = dict.computeIfAbsent(type, this::define); 117 } 118 } catch (TypeDictionary.NotDeclaredException ex) { 119 // The type has no declaration, consider it local defined 120 jt = dict.computeIfAbsent(type, this::define); 121 } 122 return jt; 123 } 124 125 /** 126 * Local lookup, the type is assumed to be locally defined. Use 127 * TypeDictionary.lookup(Type) for a global lookup or Context.getJType(Cursor) 128 * 129 * @param type 130 * @return 131 * @see TypeDictionary#lookup(Type) 132 */ 133 JType localLookup(Type type) { 134 return dict.computeIfAbsent(type, this::define); 135 } 136 137 private JType doRecord(Type t) { 138 assert(t.kind() == TypeKind.Record); 139 String name = Utils.toClassName(Utils.getIdentifier(t)); 140 Cursor dcl = t.getDeclarationCursor(); 141 // Define record locally but not declared in this file, likely a built-in type. 142 // __builtin_va_list is such a type. 143 boolean gen_code = (cf != null) && (dcl.getSourceLocation().getFileLocation().path() == null); 144 JType2 jt; 145 // case of #typedef struct Foo Bar, struct Foo is a Record type 146 // as no definition found, we consider it an annotation 147 Cursor defC = dcl.getDefinition(); 148 if (defC.isInvalid()) { 149 name = Utils.toInternalName(pkgName, clsName, name); 150 jt = JType2.bind(new TypeAlias(name, JType.Void), t, dcl); 151 } else { 152 jt = JType2.bind( 153 new JType.InnerType(Utils.toInternalName(pkgName, clsName), name), 154 t, defC); 155 if (gen_code) { 156 cf.addType(jt, defC); 157 } 158 } 159 return jt; 160 } 161 162 // Use of dict.lookup() and lookup() is tricky, if a type should have being 163 // declare earlier, use dict.lookup(); otherwise use lookup() for potentially 164 // local declaration of a type. 165 JType define(Type t) { 166 JType jt; 167 JType2 jt2; 168 logger.fine("Define " + t.kind() + ":" + t.spelling() + " for TD " + pkgName); 169 switch (t.kind()) { 170 case Unexposed: 171 jt = define(t.canonicalType()); 172 break; 173 case ConstantArray: 174 jt = new JType.Array(globalLookup(t.getElementType())); 175 break; 176 case IncompleteArray: 177 jt = new PointerType(globalLookup(t.getElementType())); 178 break; 179 case FunctionProto: 180 case FunctionNoProto: 181 JType[] args = new JType[t.numberOfArgs()]; 182 for (int i = 0; i < args.length; i++) { 183 // argument could be function pointer declared locally 184 args[i] = globalLookup(t.argType(i)); 185 } 186 jt = new JType.Function(t.isVariadic(), globalLookup(t.resultType()), args); 187 break; 188 case Enum: 189 String name = Utils.toInternalName(pkgName, clsName, 190 Utils.toClassName(Utils.getIdentifier(t))); 191 jt = new TypeAlias(name, JType.Int); 192 break; 193 case Invalid: 194 throw new IllegalArgumentException("Invalid type"); 195 case Record: 196 jt = doRecord(t); 197 break; 198 case Pointer: 199 Type pointee = t.getPointeeType(); 200 jt2 = (JType2) globalLookup(pointee); 201 jt = jt2.getDelegate(); 202 if (jt instanceof JType.Function) { 203 jt = new JType.FnIf(new JType.InnerType( 204 Utils.toInternalName(pkgName, clsName), 205 "FI" + serialNo()), 206 (JType.Function) jt); 207 if (cf != null) { 208 cf.addType(JType2.bind(jt, t, null), null); 209 } 210 } else { 211 jt = new PointerType(jt); 212 } 213 break; 214 case Typedef: 215 Type truetype = t.canonicalType(); 216 logger.fine(() -> "Typedef " + t.spelling() + " as " + truetype.spelling()); 217 name = Utils.toInternalName(pkgName, clsName, 218 Utils.toClassName(t.spelling())); 219 jt = new TypeAlias(name, globalLookup(truetype)); 220 break; 221 case BlockPointer: 222 // FIXME: what is BlockPointer? A FunctionalPointer as this is closure 223 pointee = t.getPointeeType(); 224 jt2 = (JType2) globalLookup(pointee); 225 jt = jt2.getDelegate(); 226 jt = new JType.FnIf(new JType.InnerType( 227 Utils.toInternalName(pkgName, clsName), 228 "FI" + serialNo()), 229 (JType.Function) jt); 230 if (cf != null) { 231 cf.addType(JType2.bind(jt, t, null), null); 232 } 233 break; 234 default: 235 throw new UnsupportedOperationException("Type kind not supported: " + t.kind()); 236 } 237 238 final JType finalJt = jt; 239 logger.config(() -> "Type " + t.spelling() + " defined as " + finalJt.getSignature()); 240 return (jt instanceof JType2) ? jt : JType2.bind(jt, t, t.getDeclarationCursor()); 241 } 242 } --- EOF ---