21 * questions.
22 */
23 package com.sun.tools.jextract;
24
25 import java.io.IOException;
26 import java.foreign.layout.Layout;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.logging.Logger;
37 import java.util.stream.Stream;
38 import jdk.internal.clang.SourceLocation;
39 import jdk.internal.clang.Type;
40 import jdk.internal.clang.TypeKind;
41 import jdk.internal.foreign.Util;
42 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
43 import jdk.internal.org.objectweb.asm.ClassVisitor;
44 import jdk.internal.org.objectweb.asm.ClassWriter;
45 import jdk.internal.org.objectweb.asm.MethodVisitor;
46 import jdk.internal.org.objectweb.asm.TypeReference;
47 import com.sun.tools.jextract.tree.EnumTree;
48 import com.sun.tools.jextract.tree.FieldTree;
49 import com.sun.tools.jextract.tree.FunctionTree;
50 import com.sun.tools.jextract.tree.MacroTree;
51 import com.sun.tools.jextract.tree.SimpleTreeVisitor;
52 import com.sun.tools.jextract.tree.StructTree;
53 import com.sun.tools.jextract.tree.Tree;
54 import com.sun.tools.jextract.tree.TypedefTree;
55 import com.sun.tools.jextract.tree.VarTree;
56
57 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT;
58 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ANNOTATION;
59 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_FINAL;
60 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_INTERFACE;
61 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
516 } catch (Exception ex) {
517 handleException(ex);
518 logger.warning("Tree causing above exception is: " + tree.name());
519 logger.warning(() -> tree.toString());
520 }
521 return this;
522 }
523
524 @Override
525 public Void visitMacro(MacroTree macroTree, JType jt) {
526 if (!macroTree.isConstant()) {
527 logger.fine(() -> "Skipping unrecognized object-like macro " + macroTree.name());
528 return null;
529 }
530 String name = macroTree.name();
531 Object value = macroTree.value().get();
532 if (! global_macros.add(name)) {
533 return null; // added already
534 }
535 logger.fine(() -> "Adding macro " + name);
536 Class<?> macroType = (Class<?>) Util.unboxIfNeeded(value.getClass());
537
538 String sig = jdk.internal.org.objectweb.asm.Type.getMethodDescriptor(jdk.internal.org.objectweb.asm.Type.getType(macroType));
539 MethodVisitor mv = global_cw.visitMethod(ACC_PUBLIC, name, sig, sig, null);
540
541 AnnotationVisitor av = mv.visitAnnotation(NATIVE_LOCATION, true);
542 SourceLocation src = macroTree.location();
543 SourceLocation.Location loc = src.getFileLocation();
544 Path p = loc.path();
545 av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString());
546 av.visit("line", loc.line());
547 av.visit("column", loc.column());
548 av.visit("USR", macroTree.USR());
549 av.visitEnd();
550
551 mv.visitCode();
552
553 mv.visitLdcInsn(value);
554 if (macroType.equals(char.class)) {
555 mv.visitInsn(I2C);
556 mv.visitInsn(IRETURN);
|
21 * questions.
22 */
23 package com.sun.tools.jextract;
24
25 import java.io.IOException;
26 import java.foreign.layout.Layout;
27 import java.nio.file.Path;
28 import java.nio.file.Paths;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.HashMap;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.logging.Logger;
37 import java.util.stream.Stream;
38 import jdk.internal.clang.SourceLocation;
39 import jdk.internal.clang.Type;
40 import jdk.internal.clang.TypeKind;
41 import jdk.internal.org.objectweb.asm.AnnotationVisitor;
42 import jdk.internal.org.objectweb.asm.ClassVisitor;
43 import jdk.internal.org.objectweb.asm.ClassWriter;
44 import jdk.internal.org.objectweb.asm.MethodVisitor;
45 import jdk.internal.org.objectweb.asm.TypeReference;
46 import com.sun.tools.jextract.tree.EnumTree;
47 import com.sun.tools.jextract.tree.FieldTree;
48 import com.sun.tools.jextract.tree.FunctionTree;
49 import com.sun.tools.jextract.tree.MacroTree;
50 import com.sun.tools.jextract.tree.SimpleTreeVisitor;
51 import com.sun.tools.jextract.tree.StructTree;
52 import com.sun.tools.jextract.tree.Tree;
53 import com.sun.tools.jextract.tree.TypedefTree;
54 import com.sun.tools.jextract.tree.VarTree;
55
56 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT;
57 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ANNOTATION;
58 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_FINAL;
59 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_INTERFACE;
60 import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
515 } catch (Exception ex) {
516 handleException(ex);
517 logger.warning("Tree causing above exception is: " + tree.name());
518 logger.warning(() -> tree.toString());
519 }
520 return this;
521 }
522
523 @Override
524 public Void visitMacro(MacroTree macroTree, JType jt) {
525 if (!macroTree.isConstant()) {
526 logger.fine(() -> "Skipping unrecognized object-like macro " + macroTree.name());
527 return null;
528 }
529 String name = macroTree.name();
530 Object value = macroTree.value().get();
531 if (! global_macros.add(name)) {
532 return null; // added already
533 }
534 logger.fine(() -> "Adding macro " + name);
535 Class<?> macroType = Utils.unboxIfNeeded(value.getClass());
536
537 String sig = jdk.internal.org.objectweb.asm.Type.getMethodDescriptor(jdk.internal.org.objectweb.asm.Type.getType(macroType));
538 MethodVisitor mv = global_cw.visitMethod(ACC_PUBLIC, name, sig, sig, null);
539
540 AnnotationVisitor av = mv.visitAnnotation(NATIVE_LOCATION, true);
541 SourceLocation src = macroTree.location();
542 SourceLocation.Location loc = src.getFileLocation();
543 Path p = loc.path();
544 av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString());
545 av.visit("line", loc.line());
546 av.visit("column", loc.column());
547 av.visit("USR", macroTree.USR());
548 av.visitEnd();
549
550 mv.visitCode();
551
552 mv.visitLdcInsn(value);
553 if (macroType.equals(char.class)) {
554 mv.visitInsn(I2C);
555 mv.visitInsn(IRETURN);
|