52 public AsmCodeFactory(HeaderFile header) { 53 logger.info(() -> "Instantiate AsmCodeFactory for " + header.path); 54 owner = header; 55 internal_name = Utils.toInternalName(owner.pkgName, owner.clsName); 56 global_cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); 57 types = new HashMap<>(); 58 init(); 59 } 60 61 private void init() { 62 global_cw.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, 63 internal_name, 64 null, "java/lang/Object", null); 65 AnnotationVisitor av = global_cw.visitAnnotation( 66 "Ljava/nicl/metadata/Header;", true); 67 av.visit("path", owner.path.toAbsolutePath().toString()); 68 av.visitEnd(); 69 if (owner.libraries != null && !owner.libraries.isEmpty()) { 70 AnnotationVisitor deps = global_cw.visitAnnotation( 71 "Ljava/nicl/metadata/LibraryDependencies;", true); 72 AnnotationVisitor libraries = deps.visitArray("value"); 73 for (String lib : owner.libraries) { 74 AnnotationVisitor dep = libraries.visitAnnotation(null, 75 "Ljava/nicl/metadata/LibraryDependency;"); 76 dep.visit("name", lib); 77 dep.visitEnd(); 78 } 79 libraries.visitEnd(); 80 deps.visitEnd(); 81 } 82 } 83 84 private void handleException(Exception ex) { 85 System.err.println(Main.format("cannot.write.class.file", owner.pkgName + "." + owner.clsName, ex)); 86 if (Main.DEBUG) { 87 ex.printStackTrace(System.err); 88 } 89 } 90 91 private void annotateC(ClassVisitor cw, Cursor dcl) { 92 AnnotationVisitor av = cw.visitAnnotation( 93 "Ljava/nicl/metadata/C;", true); 94 SourceLocation src = dcl.getSourceLocation(); 95 SourceLocation.Location loc = src.getFileLocation(); 96 Path p = loc.path(); 97 av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString()); 98 av.visit("line", loc.line()); 99 av.visit("column", loc.column()); | 52 public AsmCodeFactory(HeaderFile header) { 53 logger.info(() -> "Instantiate AsmCodeFactory for " + header.path); 54 owner = header; 55 internal_name = Utils.toInternalName(owner.pkgName, owner.clsName); 56 global_cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); 57 types = new HashMap<>(); 58 init(); 59 } 60 61 private void init() { 62 global_cw.visit(V1_8, ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, 63 internal_name, 64 null, "java/lang/Object", null); 65 AnnotationVisitor av = global_cw.visitAnnotation( 66 "Ljava/nicl/metadata/Header;", true); 67 av.visit("path", owner.path.toAbsolutePath().toString()); 68 av.visitEnd(); 69 if (owner.libraries != null && !owner.libraries.isEmpty()) { 70 AnnotationVisitor deps = global_cw.visitAnnotation( 71 "Ljava/nicl/metadata/LibraryDependencies;", true); 72 AnnotationVisitor libNames = deps.visitArray("names"); 73 for (String name : owner.libraries) { 74 libNames.visit(null, name); 75 } 76 libNames.visitEnd(); 77 if (owner.libraryPaths != null && !owner.libraryPaths.isEmpty()) { 78 AnnotationVisitor libPaths = deps.visitArray("paths"); 79 for (String path : owner.libraryPaths) { 80 libPaths.visit(null, path); 81 } 82 libPaths.visitEnd(); 83 } 84 deps.visitEnd(); 85 } 86 } 87 88 private void handleException(Exception ex) { 89 System.err.println(Main.format("cannot.write.class.file", owner.pkgName + "." + owner.clsName, ex)); 90 if (Main.DEBUG) { 91 ex.printStackTrace(System.err); 92 } 93 } 94 95 private void annotateC(ClassVisitor cw, Cursor dcl) { 96 AnnotationVisitor av = cw.visitAnnotation( 97 "Ljava/nicl/metadata/C;", true); 98 SourceLocation src = dcl.getSourceLocation(); 99 SourceLocation.Location loc = src.getFileLocation(); 100 Path p = loc.path(); 101 av.visit("file", p == null ? "builtin" : p.toAbsolutePath().toString()); 102 av.visit("line", loc.line()); 103 av.visit("column", loc.column()); |