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