func (g *objcGen) genInterfaceM(obj *types.TypeName, t *types.Interface) bool { summary := makeIfaceSummary(t) // @implementation Interface -- similar to what genStructM does. g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name()) g.Printf("}\n") g.Printf("\n") g.Printf("- (id)initWithRef:(id)ref {\n") g.Indent() g.Printf("self = [super init];\n") g.Printf("if (self) { __ref = ref; }\n") g.Printf("return self;\n") g.Outdent() g.Printf("}\n") g.Printf("\n") for _, m := range summary.callable { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) continue } s := g.funcSummary(m) g.Printf("- %s {\n", s.asMethod(g)) g.Indent() g.genFunc(s, obj.Name()) g.Outdent() g.Printf("}\n\n") } g.Printf("@end\n") g.Printf("\n") return summary.implementable }
func (g *JavaGen) genJNIField(o *types.TypeName, f *types.Var) { if t := f.Type(); !g.isSupported(t) { g.Printf("// skipped field %s with unsupported type: %T\n\n", o.Name(), t) return } // setter g.Printf("JNIEXPORT void JNICALL\n") g.Printf("Java_%s_%s_set%s(JNIEnv *env, jobject this, %s v) {\n", g.jniPkgName(), o.Name(), f.Name(), g.jniType(f.Type())) g.Indent() g.Printf("int32_t o = go_seq_to_refnum_go(env, this);\n") g.genJavaToC("v", f.Type(), modeRetained) g.Printf("proxy%s_%s_%s_Set(o, _v);\n", g.pkgPrefix, o.Name(), f.Name()) g.genRelease("v", f.Type(), modeRetained) g.Outdent() g.Printf("}\n\n") // getter g.Printf("JNIEXPORT %s JNICALL\n", g.jniType(f.Type())) g.Printf("Java_%s_%s_get%s(JNIEnv *env, jobject this) {\n", g.jniPkgName(), o.Name(), f.Name()) g.Indent() g.Printf("int32_t o = go_seq_to_refnum_go(env, this);\n") g.Printf("%s r0 = ", g.cgoType(f.Type())) g.Printf("proxy%s_%s_%s_Get(o);\n", g.pkgPrefix, o.Name(), f.Name()) g.genCToJava("_r0", "r0", f.Type(), modeRetained) g.Printf("return _r0;\n") g.Outdent() g.Printf("}\n\n") }
func (g *objcGen) genInterfaceH(obj *types.TypeName, t *types.Interface) { g.Printf("@protocol %s%s\n", g.namePrefix, obj.Name()) for _, m := range exportedMethodSet(obj.Type()) { s := g.funcSummary(m) g.Printf("- %s;\n", s.asMethod(g)) } g.Printf("@end\n") }
func (g *javaGen) genFromRefnum(toName, fromName string, t types.Type, o *types.TypeName) { oPkg := o.Pkg() if !g.validPkg(oPkg) { g.errorf("type %s is defined in package %s, which is not bound", t, oPkg) return } p := pkgPrefix(oPkg) g.Printf("jobject %s = go_seq_from_refnum(env, %s, proxy_class_%s_%s, proxy_class_%s_%s_cons);\n", toName, fromName, p, o.Name(), p, o.Name()) }
func (g *ObjcGen) genStructM(obj *types.TypeName, t *types.Struct) { fields := exportedFields(t) methods := exportedMethodSet(types.NewPointer(obj.Type())) g.Printf("\n") oinf := g.ostructs[obj] g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name()) g.Printf("}\n\n") g.Printf("- (id)initWithRef:(id)ref {\n") g.Indent() g.Printf("self = [super init];\n") g.Printf("if (self) { __ref = ref; }\n") g.Printf("return self;\n") g.Outdent() g.Printf("}\n\n") if oinf != nil { g.Printf("- (id)init {\n") g.Indent() g.Printf("self = [super init];\n") g.Printf("if (self) {\n") g.Indent() g.Printf("__ref = go_seq_from_refnum(new_%s_%s());\n", g.pkgPrefix, obj.Name()) g.Outdent() g.Printf("}\n") g.Printf("return self;\n") g.Outdent() g.Printf("}\n\n") } for _, f := range fields { if !g.isSupported(f.Type()) { g.Printf("// skipped unsupported field %s with type %T\n\n", f.Name(), f) continue } g.genGetter(obj.Name(), f) g.genSetter(obj.Name(), f) } for _, m := range methods { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) continue } s := g.funcSummary(g.ostructs[obj], m) g.Printf("- %s {\n", s.asMethod(g)) g.Indent() g.genFunc(s, obj.Name()) g.Outdent() g.Printf("}\n\n") } g.Printf("@end\n\n") }
func (g *objcGen) genInterfaceM2(obj *types.TypeName, t *types.Interface) bool { summary := makeIfaceSummary(t) desc := fmt.Sprintf("_GO_%s_%s", g.pkgName, obj.Name()) // @implementation Interface -- similar to what genStructM does. g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name()) g.Printf("}\n") g.Printf("\n") g.Printf("- (id)initWithRef:(id)ref {\n") g.Indent() g.Printf("self = [super init];\n") g.Printf("if (self) { _ref = ref; }\n") g.Printf("return self;\n") g.Outdent() g.Printf("}\n") g.Printf("\n") for _, m := range summary.callable { s := g.funcSummary(m) g.Printf("- %s {\n", s.asMethod(g)) g.Indent() g.genFunc(desc+"_DESCRIPTOR_", desc+"_"+m.Name()+"_", s, true) g.Outdent() g.Printf("}\n\n") } g.Printf("@end\n") g.Printf("\n") // proxy function. if summary.implementable { g.Printf("static void proxy%s%s(id obj, int code, GoSeq* in, GoSeq* out) {\n", g.namePrefix, obj.Name()) g.Indent() g.Printf("switch (code) {\n") for _, m := range summary.callable { g.Printf("case %s_%s_: {\n", desc, m.Name()) g.Indent() g.genInterfaceMethodProxy(obj, g.funcSummary(m)) g.Outdent() g.Printf("} break;\n") } g.Printf("default:\n") g.Indent() g.Printf("NSLog(@\"unknown code %%x for %s_DESCRIPTOR_\", code);\n", desc) g.Outdent() g.Printf("}\n") g.Outdent() g.Printf("}\n") } return summary.implementable }
func (g *objcGen) genInterfaceH(obj *types.TypeName, t *types.Interface) { summary := makeIfaceSummary(t) if !summary.implementable { g.genInterfaceInterface(obj, summary, false) return } g.Printf("@protocol %s%s\n", g.namePrefix, obj.Name()) for _, m := range makeIfaceSummary(t).callable { s := g.funcSummary(m) g.Printf("- %s;\n", s.asMethod(g)) } g.Printf("@end\n") }
func newStruct(p *Package, obj *types.TypeName) (Struct, error) { sym := p.syms.symtype(obj.Type()) if sym == nil { panic(fmt.Errorf("no such object [%s] in symbols table", obj.Id())) } sym.doc = p.getDoc("", obj) s := Struct{ pkg: p, sym: sym, obj: obj, } return s, nil }
func (g *objcGen) genInterfaceInterface(obj *types.TypeName, summary ifaceSummary, isProtocol bool) { g.Printf("@interface %[1]s%[2]s : NSObject", g.namePrefix, obj.Name()) if isProtocol { g.Printf(" <%[1]s%[2]s>", g.namePrefix, obj.Name()) } g.Printf(" {\n}\n") g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("\n") g.Printf("- (id)initWithRef:(id)ref;\n") for _, m := range summary.callable { s := g.funcSummary(m) g.Printf("- %s;\n", s.asMethod(g)) } g.Printf("@end\n") }
func (g *JavaGen) genFromRefnum(toName, fromName string, t types.Type, o *types.TypeName) { oPkg := o.Pkg() isJava := isJavaType(o.Type()) if !isErrorType(o.Type()) && !g.validPkg(oPkg) && !isJava { g.errorf("type %s is defined in package %s, which is not bound", t, oPkg) return } p := pkgPrefix(oPkg) g.Printf("jobject %s = go_seq_from_refnum(env, %s, ", toName, fromName) if isJava { g.Printf("NULL, NULL") } else { g.Printf("proxy_class_%s_%s, proxy_class_%s_%s_cons", p, o.Name(), p, o.Name()) } g.Printf(");\n") }
func (g *ObjcGen) genInterfaceH(obj *types.TypeName, t *types.Interface) { summary := makeIfaceSummary(t) if !summary.implementable { g.genInterfaceInterface(obj, summary, false) return } g.Printf("@protocol %s%s <NSObject>\n", g.namePrefix, obj.Name()) for _, m := range makeIfaceSummary(t).callable { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) continue } s := g.funcSummary(nil, m) g.Printf("- %s;\n", s.asMethod(g)) } g.Printf("@end\n") }
// methodsFor returns the named type and corresponding methods if the type // denoted by obj is not an interface and has methods. Otherwise it returns // the zero value. func methodsFor(obj *types.TypeName) (*types.Named, []*types.Selection) { named, _ := obj.Type().(*types.Named) if named == nil { // A type name's type can also be the // exported basic type unsafe.Pointer. return nil, nil } if _, ok := named.Underlying().(*types.Interface); ok { // ignore interfaces return nil, nil } methods := combinedMethodSet(named) if len(methods) == 0 { return nil, nil } return named, methods }
func (g *objcGen) genInterfaceM1(obj *types.TypeName, t *types.Interface) bool { summary := makeIfaceSummary(t) desc := fmt.Sprintf("_GO_%s_%s", g.pkgName, obj.Name()) g.Printf("#define %s_DESCRIPTOR_ \"go.%s.%s\"\n", desc, g.pkgName, obj.Name()) for i, m := range summary.callable { g.Printf("#define %s_%s_ (0x%x0a)\n", desc, m.Name(), i+1) } g.Printf("\n") if summary.implementable { // @interface Interface -- similar to what genStructH does. g.genInterfaceInterface(obj, summary, true) } return summary.implementable }
func methodDocComment(prog *loader.Program, tname *types.TypeName, methodName string) (string, error) { t := tname.Type() if !types.IsInterface(t) { // Use the pointer type to get as many methods as possible. t = types.NewPointer(t) } mset := types.NewMethodSet(t) sel := mset.Lookup(nil, methodName) if sel == nil { return "", errgo.Newf("cannot find method %v on %v", methodName, t) } obj := sel.Obj() decl, err := findDecl(prog, obj.Pos()) if err != nil { return "", errgo.Mask(err) } switch decl := decl.(type) { case *ast.GenDecl: if decl.Tok != token.TYPE { return "", errgo.Newf("found non-type decl %#v", decl) } for _, spec := range decl.Specs { tspec := spec.(*ast.TypeSpec) it := tspec.Type.(*ast.InterfaceType) for _, m := range it.Methods.List { for _, id := range m.Names { if id.Pos() == obj.Pos() { return m.Doc.Text(), nil } } } } return "", errgo.Newf("method definition not found in type") case *ast.FuncDecl: if decl.Name.Pos() != obj.Pos() { return "", errgo.Newf("method definition not found (at %#v)", prog.Fset.Position(obj.Pos())) } return decl.Doc.Text(), nil default: return "", errgo.Newf("unexpected declaration %T found", decl) } }
func (g *objcGen) genInterfaceInterface(obj *types.TypeName, summary ifaceSummary, isProtocol bool) { g.Printf("@interface %[1]s%[2]s : NSObject", g.namePrefix, obj.Name()) if isProtocol { g.Printf(" <%[1]s%[2]s>", g.namePrefix, obj.Name()) } g.Printf(" {\n}\n") g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("\n") g.Printf("- (id)initWithRef:(id)ref;\n") for _, m := range summary.callable { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) return } s := g.funcSummary(m) g.Printf("- %s;\n", s.asMethod(g)) } g.Printf("@end\n") }
func typeDocComment(prog *loader.Program, t *types.TypeName) (string, error) { decl, err := findDecl(prog, t.Pos()) if err != nil { return "", errgo.Mask(err) } tdecl, ok := decl.(*ast.GenDecl) if !ok || tdecl.Tok != token.TYPE { return "", errgo.Newf("found non-type decl %#v", decl) } for _, spec := range tdecl.Specs { tspec := spec.(*ast.TypeSpec) if tspec.Name.Pos() == t.Pos() { if tspec.Doc != nil { return tspec.Doc.Text(), nil } return tdecl.Doc.Text(), nil } } return "", errgo.Newf("cannot find type declaration") }
func (g *objcGen) genStructH(obj *types.TypeName, t *types.Struct) { g.Printf("@interface %s%s : NSObject {\n", g.namePrefix, obj.Name()) g.Printf("}\n") g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("\n") g.Printf("- (id)initWithRef:(id)ref;\n") // accessors to exported fields. for _, f := range exportedFields(t) { name, typ := f.Name(), g.objcFieldType(f.Type()) g.Printf("- (%s)%s;\n", typ, lowerFirst(name)) g.Printf("- (void)set%s:(%s)v;\n", name, typ) } // exported methods for _, m := range exportedMethodSet(types.NewPointer(obj.Type())) { s := g.funcSummary(m) g.Printf("- %s;\n", lowerFirst(s.asMethod(g))) } g.Printf("@end\n") }
func (g *objcGen) genStructH(obj *types.TypeName, t *types.Struct) { g.Printf("@interface %s%s : NSObject {\n", g.namePrefix, obj.Name()) g.Printf("}\n") g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("\n") g.Printf("- (id)initWithRef:(id)ref;\n") // accessors to exported fields. for _, f := range exportedFields(t) { if t := f.Type(); !g.isSupported(t) { g.Printf("// skipped field %s.%s with unsupported type: %T\n\n", obj.Name(), f.Name(), t) continue } name, typ := f.Name(), g.objcFieldType(f.Type()) g.Printf("- (%s)%s;\n", typ, lowerFirst(name)) g.Printf("- (void)set%s:(%s)v;\n", name, typ) } // exported methods for _, m := range exportedMethodSet(types.NewPointer(obj.Type())) { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) continue } s := g.funcSummary(m) g.Printf("- %s;\n", lowerFirst(s.asMethod(g))) } g.Printf("@end\n") }
func (g *ObjcGen) genInterfaceInterface(obj *types.TypeName, summary ifaceSummary, isProtocol bool) { g.Printf("@interface %[1]s%[2]s : ", g.namePrefix, obj.Name()) if isErrorType(obj.Type()) { g.Printf("NSError") } else { g.Printf("NSObject") } prots := []string{"goSeqRefInterface"} if isProtocol { prots = append(prots, fmt.Sprintf("%[1]s%[2]s", g.namePrefix, obj.Name())) } g.Printf(" <%s>", strings.Join(prots, ", ")) g.Printf(" {\n}\n") g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("\n") g.Printf("- (instancetype)initWithRef:(id)ref;\n") for _, m := range summary.callable { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) continue } s := g.funcSummary(nil, m) g.Printf("- %s;\n", s.asMethod(g)) } g.Printf("@end\n") }
func (w *Walker) emitType(obj *types.TypeName) { name := obj.Name() typ := obj.Type() switch typ := typ.Underlying().(type) { case *types.Struct: w.emitStructType(name, typ) case *types.Interface: w.emitIfaceType(name, typ) return // methods are handled by emitIfaceType default: w.emitf("type %s %s", name, w.typeString(typ.Underlying())) } // emit methods with value receiver var methodNames map[string]bool vset := types.NewMethodSet(typ) for i, n := 0, vset.Len(); i < n; i++ { m := vset.At(i) if m.Obj().Exported() { w.emitMethod(m) if methodNames == nil { methodNames = make(map[string]bool) } methodNames[m.Obj().Name()] = true } } // emit methods with pointer receiver; exclude // methods that we have emitted already // (the method set of *T includes the methods of T) pset := types.NewMethodSet(types.NewPointer(typ)) for i, n := 0, pset.Len(); i < n; i++ { m := pset.At(i) if m.Obj().Exported() && !methodNames[m.Obj().Name()] { w.emitMethod(m) } } }
func (g *objcGen) genStructM(obj *types.TypeName, t *types.Struct) { fields := exportedFields(t) methods := exportedMethodSet(types.NewPointer(obj.Type())) desc := fmt.Sprintf("_GO_%s_%s", g.pkgName, obj.Name()) g.Printf("#define %s_DESCRIPTOR_ \"go.%s.%s\"\n", desc, g.pkgName, obj.Name()) for i, f := range fields { g.Printf("#define %s_FIELD_%s_GET_ (0x%x0f)\n", desc, f.Name(), i) g.Printf("#define %s_FIELD_%s_SET_ (0x%x1f)\n", desc, f.Name(), i) } for i, m := range methods { g.Printf("#define %s_%s_ (0x%x0c)\n", desc, m.Name(), i) } g.Printf("\n") g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name()) g.Printf("}\n\n") g.Printf("- (id)initWithRef:(id)ref {\n") g.Indent() g.Printf("self = [super init];\n") g.Printf("if (self) { __ref = ref; }\n") g.Printf("return self;\n") g.Outdent() g.Printf("}\n\n") for _, f := range fields { g.genGetter(desc, f) g.genSetter(desc, f) } for _, m := range methods { s := g.funcSummary(m) g.Printf("- %s {\n", s.asMethod(g)) g.Indent() g.genFunc(desc+"_DESCRIPTOR_", desc+"_"+m.Name()+"_", s, true) g.Outdent() g.Printf("}\n\n") } g.Printf("@end\n") }
func (g *ObjcGen) genStructH(obj *types.TypeName, t *types.Struct) { g.Printf("@interface %s%s : ", g.namePrefix, obj.Name()) oinf := g.ostructs[obj] if oinf != nil { var prots []string for _, sup := range oinf.supers { if !sup.Protocol { g.Printf(sup.Name) } else { prots = append(prots, sup.Name) } } if len(prots) > 0 { g.Printf(" <%s>", strings.Join(prots, ", ")) } } else { g.Printf("NSObject <goSeqRefInterface>") } g.Printf(" {\n") g.Printf("}\n") g.Printf("@property(strong, readonly) id _ref;\n") g.Printf("\n") g.Printf("- (id)initWithRef:(id)ref;\n") if oinf != nil { g.Printf("- (id)init;\n") } // accessors to exported fields. for _, f := range exportedFields(t) { if t := f.Type(); !g.isSupported(t) { g.Printf("// skipped field %s.%s with unsupported type: %T\n\n", obj.Name(), f.Name(), t) continue } name, typ := f.Name(), g.objcFieldType(f.Type()) g.Printf("- (%s)%s;\n", typ, objcNameReplacer(lowerFirst(name))) g.Printf("- (void)set%s:(%s)v;\n", name, typ) } // exported methods for _, m := range exportedMethodSet(types.NewPointer(obj.Type())) { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) continue } s := g.funcSummary(g.ostructs[obj], m) g.Printf("- %s;\n", objcNameReplacer(lowerFirst(s.asMethod(g)))) } g.Printf("@end\n") }
func (g *javaGen) genInterface(o *types.TypeName) { iface := o.Type().(*types.Named).Underlying().(*types.Interface) summary := makeIfaceSummary(iface) g.Printf("public interface %s extends go.Seq.Object {\n", o.Name()) g.Indent() methodSigErr := false for _, m := range summary.callable { if err := g.funcSignature(m, false); err != nil { methodSigErr = true g.errorf("%v", err) } g.Printf(";\n\n") } if methodSigErr { return // skip stub generation, more of the same errors } if summary.implementable { g.genInterfaceStub(o, iface) } g.Printf(javaProxyPreamble, o.Name()) g.Indent() for _, m := range summary.callable { g.genFunc(m, true) } for i, m := range summary.callable { g.Printf("static final int CALL_%s = 0x%x0a;\n", m.Name(), i+1) } g.Outdent() g.Printf("}\n") g.Outdent() g.Printf("}\n\n") }
func (g *ObjcGen) genInterfaceM(obj *types.TypeName, t *types.Interface) bool { summary := makeIfaceSummary(t) // @implementation Interface -- similar to what genStructM does. g.Printf("@implementation %s%s {\n", g.namePrefix, obj.Name()) g.Printf("}\n") g.Printf("\n") g.Printf("- (instancetype)initWithRef:(id)ref {\n") g.Indent() if isErrorType(obj.Type()) { g.Printf("if (self) {\n") g.Printf(" __ref = ref;\n") g.Printf(" self = [super initWithDomain:@\"go\" code:1 userInfo:@{NSLocalizedDescriptionKey: [self error]}];\n") g.Printf("}\n") } else { g.Printf("self = [super init];\n") g.Printf("if (self) { __ref = ref; }\n") } g.Printf("return self;\n") g.Outdent() g.Printf("}\n") g.Printf("\n") for _, m := range summary.callable { if !g.isSigSupported(m.Type()) { g.Printf("// skipped method %s.%s with unsupported parameter or return types\n\n", obj.Name(), m.Name()) continue } s := g.funcSummary(nil, m) g.Printf("- %s {\n", s.asMethod(g)) g.Indent() g.genFunc(s, obj.Name()) g.Outdent() g.Printf("}\n\n") } g.Printf("@end\n") g.Printf("\n") return summary.implementable }
func (g *javaGen) genInterface(o *types.TypeName) { iface := o.Type().(*types.Named).Underlying().(*types.Interface) g.Printf("public interface %s extends go.Seq.Object {\n", o.Name()) g.Indent() methodSigErr := false for i := 0; i < iface.NumMethods(); i++ { if err := g.funcSignature(iface.Method(i), false); err != nil { methodSigErr = true g.errorf("%v", err) } g.Printf(";\n\n") } if methodSigErr { return // skip stub generation, more of the same errors } g.genInterfaceStub(o, iface) g.Printf(javaProxyPreamble, o.Name()) g.Indent() for i := 0; i < iface.NumMethods(); i++ { g.genFunc(iface.Method(i), true) } for i := 0; i < iface.NumMethods(); i++ { g.Printf("static final int CALL_%s = 0x%x0a;\n", iface.Method(i).Name(), i+1) } g.Outdent() g.Printf("}\n") g.Outdent() g.Printf("}\n\n") }
func (c *converter) convertTypeName(v *gotypes.TypeName) *types.TypeName { if v == nil { return nil } if v, ok := c.converted[v]; ok { return v.(*types.TypeName) } // This part is a bit tricky. gcimport calls NewTypeName with a nil typ // argument, and then calls NewNamed on the resulting *TypeName, which // sets its typ to a *Named referring to itself. So if we get a *TypeName // whose Type() is a *Named whose Obj() is the same *TypeName, we know it // was constructed this way, so we do the same. Otherwise we get into a // infinite recursion converting the *TypeName's type. var typ types.Type if named, ok := v.Type().(*gotypes.Named); !ok || named.Obj() != v { typ = c.convertType(v.Type()) } ret := types.NewTypeName( token.Pos(v.Pos()), c.convertPackage(v.Pkg()), v.Name(), typ, ) c.converted[v] = ret named := types.NewNamed(ret, c.convertType(v.Type().Underlying()), nil) c.converted[v.Type()] = named return ret }
func (g *javaGen) genStruct(obj *types.TypeName, T *types.Struct) { fields := exportedFields(T) methods := exportedMethodSet(types.NewPointer(obj.Type())) g.Printf("public static final class %s implements go.Seq.Object {\n", obj.Name()) g.Indent() g.Printf("private static final String DESCRIPTOR = \"go.%s.%s\";\n", g.pkg.Name(), obj.Name()) for i, f := range fields { g.Printf("private static final int FIELD_%s_GET = 0x%x0f;\n", f.Name(), i) g.Printf("private static final int FIELD_%s_SET = 0x%x1f;\n", f.Name(), i) } for i, m := range methods { g.Printf("private static final int CALL_%s = 0x%x0c;\n", m.Name(), i) } g.Printf("\n") g.Printf("private go.Seq.Ref ref;\n\n") n := obj.Name() g.Printf("private %s(go.Seq.Ref ref) { this.ref = ref; }\n\n", n) g.Printf(`public go.Seq.Ref ref() { return ref; } public void call(int code, go.Seq in, go.Seq out) { throw new RuntimeException("internal error: cycle: cannot call concrete proxy"); } `) for _, f := range fields { g.Printf("public %s get%s() {\n", g.javaType(f.Type()), f.Name()) g.Indent() g.Printf("Seq in = new Seq();\n") g.Printf("Seq out = new Seq();\n") g.Printf("in.writeRef(ref);\n") g.Printf("Seq.send(DESCRIPTOR, FIELD_%s_GET, in, out);\n", f.Name()) if seqType(f.Type()) == "Ref" { g.Printf("return new %s(out.read%s);\n", g.javaType(f.Type()), seqRead(f.Type())) } else { g.Printf("return out.read%s;\n", seqRead(f.Type())) } g.Outdent() g.Printf("}\n\n") g.Printf("public void set%s(%s v) {\n", f.Name(), g.javaType(f.Type())) g.Indent() g.Printf("Seq in = new Seq();\n") g.Printf("Seq out = new Seq();\n") g.Printf("in.writeRef(ref);\n") g.Printf("in.write%s;\n", seqWrite(f.Type(), "v")) g.Printf("Seq.send(DESCRIPTOR, FIELD_%s_SET, in, out);\n", f.Name()) g.Outdent() g.Printf("}\n\n") } for _, m := range methods { g.genFunc(m, true) } g.Printf("@Override public boolean equals(Object o) {\n") g.Indent() g.Printf("if (o == null || !(o instanceof %s)) {\n return false;\n}\n", n) g.Printf("%s that = (%s)o;\n", n, n) for _, f := range fields { nf := f.Name() g.Printf("%s this%s = get%s();\n", g.javaType(f.Type()), nf, nf) g.Printf("%s that%s = that.get%s();\n", g.javaType(f.Type()), nf, nf) if isJavaPrimitive(f.Type()) { g.Printf("if (this%s != that%s) {\n return false;\n}\n", nf, nf) } else { g.Printf("if (this%s == null) {\n", nf) g.Indent() g.Printf("if (that%s != null) {\n return false;\n}\n", nf) g.Outdent() g.Printf("} else if (!this%s.equals(that%s)) {\n return false;\n}\n", nf, nf) } } g.Printf("return true;\n") g.Outdent() g.Printf("}\n\n") g.Printf("@Override public int hashCode() {\n") g.Printf(" return java.util.Arrays.hashCode(new Object[] {") for i, f := range fields { if i > 0 { g.Printf(", ") } g.Printf("get%s()", f.Name()) } g.Printf("});\n") g.Printf("}\n\n") // TODO(crawshaw): use String() string if it is defined. g.Printf("@Override public String toString() {\n") g.Indent() g.Printf("StringBuilder b = new StringBuilder();\n") g.Printf(`b.append("%s").append("{");`, obj.Name()) g.Printf("\n") for _, f := range fields { n := f.Name() g.Printf(`b.append("%s:").append(get%s()).append(",");`, n, n) g.Printf("\n") } g.Printf(`return b.append("}").toString();`) g.Printf("\n") g.Outdent() g.Printf("}\n\n") g.Outdent() g.Printf("}\n\n") }
func (g *javaGen) genInterfaceStub(o *types.TypeName, m *types.Interface) { g.Printf("public static abstract class Stub implements %s {\n", o.Name()) g.Indent() g.Printf("static final String DESCRIPTOR = \"go.%s.%s\";\n\n", g.pkg.Name(), o.Name()) g.Printf("private final go.Seq.Ref ref;\n") g.Printf("public Stub() {\n ref = go.Seq.createRef(this);\n}\n\n") g.Printf("public go.Seq.Ref ref() { return ref; }\n\n") g.Printf("public void call(int code, go.Seq in, go.Seq out) {\n") g.Indent() g.Printf("switch (code) {\n") for i := 0; i < m.NumMethods(); i++ { f := m.Method(i) g.Printf("case Proxy.CALL_%s: {\n", f.Name()) g.Indent() sig := f.Type().(*types.Signature) params := sig.Params() for i := 0; i < params.Len(); i++ { p := sig.Params().At(i) jt := g.javaType(p.Type()) g.Printf("%s param_%s;\n", jt, paramName(params, i)) g.genRead("param_"+paramName(params, i), "in", p.Type()) } res := sig.Results() var returnsError bool var numRes = res.Len() if (res.Len() == 1 && isErrorType(res.At(0).Type())) || (res.Len() == 2 && isErrorType(res.At(1).Type())) { numRes -= 1 returnsError = true } if returnsError { g.Printf("try {\n") g.Indent() } if numRes > 0 { g.Printf("%s result = ", g.javaType(res.At(0).Type())) } g.Printf("this.%s(", f.Name()) for i := 0; i < params.Len(); i++ { if i > 0 { g.Printf(", ") } g.Printf("param_%s", paramName(params, i)) } g.Printf(");\n") if numRes > 0 { g.Printf("out.write%s;\n", seqWrite(res.At(0).Type(), "result")) } if returnsError { g.Printf("out.writeString(null);\n") g.Outdent() g.Printf("} catch (Exception e) {\n") g.Indent() if numRes > 0 { resTyp := res.At(0).Type() g.Printf("%s result = %s;\n", g.javaType(resTyp), g.javaTypeDefault(resTyp)) g.Printf("out.write%s;\n", seqWrite(resTyp, "result")) } g.Printf("out.writeString(e.getMessage());\n") g.Outdent() g.Printf("}\n") } g.Printf("return;\n") g.Outdent() g.Printf("}\n") } g.Printf("default:\n throw new RuntimeException(\"unknown code: \"+ code);\n") g.Printf("}\n") g.Outdent() g.Printf("}\n") g.Outdent() g.Printf("}\n\n") }
func (g *goGen) genInterface(obj *types.TypeName) { iface := obj.Type().(*types.Named).Underlying().(*types.Interface) ifaceDesc := fmt.Sprintf("go.%s.%s", g.pkg.Name(), obj.Name()) summary := makeIfaceSummary(iface) // Descriptor and code for interface methods. g.Printf("const (\n") g.Indent() g.Printf("proxy%s_Descriptor = %q\n", obj.Name(), ifaceDesc) for i, m := range summary.callable { g.Printf("proxy%s_%s_Code = 0x%x0a\n", obj.Name(), m.Name(), i+1) } g.Outdent() g.Printf(")\n\n") // Define the entry points. for _, m := range summary.callable { g.Printf("func proxy%s_%s(out, in *seq.Buffer) {\n", obj.Name(), m.Name()) g.Indent() g.Printf("ref := in.ReadRef()\n") g.Printf("v := ref.Get().(%s.%s)\n", g.pkg.Name(), obj.Name()) g.genFuncBody(m, "v") g.Outdent() g.Printf("}\n\n") } // Register the method entry points. if len(summary.callable) > 0 { g.Printf("func init() {\n") g.Indent() for _, m := range summary.callable { g.Printf("seq.Register(proxy%s_Descriptor, proxy%s_%s_Code, proxy%s_%s)\n", obj.Name(), obj.Name(), m.Name(), obj.Name(), m.Name()) } g.Outdent() g.Printf("}\n\n") } // Define a proxy interface. if !summary.implementable { // The interface defines an unexported method or a method that // uses an unexported type. We cannot generate a proxy object // for such a type. return } g.Printf("type proxy%s seq.Ref\n\n", obj.Name()) for i := 0; i < iface.NumMethods(); i++ { m := iface.Method(i) sig := m.Type().(*types.Signature) params := sig.Params() res := sig.Results() if res.Len() > 2 || (res.Len() == 2 && !isErrorType(res.At(1).Type())) { g.errorf("functions and methods must return either zero or one value, and optionally an error: %s.%s", obj.Name(), m.Name()) continue } g.Printf("func (p *proxy%s) %s(", obj.Name(), m.Name()) for i := 0; i < params.Len(); i++ { if i > 0 { g.Printf(", ") } g.Printf("%s %s", paramName(params, i), g.typeString(params.At(i).Type())) } g.Printf(") ") if res.Len() == 1 { g.Printf(g.typeString(res.At(0).Type())) } else if res.Len() == 2 { g.Printf("(%s, error)", g.typeString(res.At(0).Type())) } g.Printf(" {\n") g.Indent() g.Printf("in := new(seq.Buffer)\n") for i := 0; i < params.Len(); i++ { g.genWrite(paramName(params, i), "in", params.At(i).Type()) } if res.Len() == 0 { g.Printf("seq.Transact((*seq.Ref)(p), %q, proxy%s_%s_Code, in)\n", ifaceDesc, obj.Name(), m.Name()) } else { g.Printf("out := seq.Transact((*seq.Ref)(p), %q, proxy%s_%s_Code, in)\n", ifaceDesc, obj.Name(), m.Name()) var rvs []string for i := 0; i < res.Len(); i++ { rv := fmt.Sprintf("res_%d", i) g.genRead(rv, "out", res.At(i).Type()) rvs = append(rvs, rv) } g.Printf("return %s\n", strings.Join(rvs, ",")) } g.Outdent() g.Printf("}\n\n") } }
func (g *goGen) genStruct(obj *types.TypeName, T *types.Struct) { fields := exportedFields(T) methods := exportedMethodSet(types.NewPointer(obj.Type())) g.Printf("const (\n") g.Indent() g.Printf("proxy%s_Descriptor = \"go.%s.%s\"\n", obj.Name(), g.pkg.Name(), obj.Name()) for i, f := range fields { g.Printf("proxy%s_%s_Get_Code = 0x%x0f\n", obj.Name(), f.Name(), i) g.Printf("proxy%s_%s_Set_Code = 0x%x1f\n", obj.Name(), f.Name(), i) } for i, m := range methods { g.Printf("proxy%s_%s_Code = 0x%x0c\n", obj.Name(), m.Name(), i) } g.Outdent() g.Printf(")\n\n") g.Printf("type proxy%s seq.Ref\n\n", obj.Name()) for _, f := range fields { g.Printf("func proxy%s_%s_Set(out, in *seq.Buffer) {\n", obj.Name(), f.Name()) g.Indent() g.Printf("ref := in.ReadRef()\n") g.genRead("v", "in", f.Type()) g.Printf("ref.Get().(*%s.%s).%s = v\n", g.pkg.Name(), obj.Name(), f.Name()) g.Outdent() g.Printf("}\n\n") g.Printf("func proxy%s_%s_Get(out, in *seq.Buffer) {\n", obj.Name(), f.Name()) g.Indent() g.Printf("ref := in.ReadRef()\n") g.Printf("v := ref.Get().(*%s.%s).%s\n", g.pkg.Name(), obj.Name(), f.Name()) g.genWrite("v", "out", f.Type()) g.Outdent() g.Printf("}\n\n") } for _, m := range methods { g.Printf("func proxy%s_%s(out, in *seq.Buffer) {\n", obj.Name(), m.Name()) g.Indent() g.Printf("ref := in.ReadRef()\n") g.Printf("v := ref.Get().(*%s.%s)\n", g.pkg.Name(), obj.Name()) g.genFuncBody(m, "v") g.Outdent() g.Printf("}\n\n") } g.Printf("func init() {\n") g.Indent() for _, f := range fields { n := f.Name() g.Printf("seq.Register(proxy%s_Descriptor, proxy%s_%s_Set_Code, proxy%s_%s_Set)\n", obj.Name(), obj.Name(), n, obj.Name(), n) g.Printf("seq.Register(proxy%s_Descriptor, proxy%s_%s_Get_Code, proxy%s_%s_Get)\n", obj.Name(), obj.Name(), n, obj.Name(), n) } for _, m := range methods { n := m.Name() g.Printf("seq.Register(proxy%s_Descriptor, proxy%s_%s_Code, proxy%s_%s)\n", obj.Name(), obj.Name(), n, obj.Name(), n) } g.Outdent() g.Printf("}\n\n") }