func (g *Grapher) assignMethodPaths(named *types.Named, prefix []string, pkgscope bool) { for i := 0; i < named.NumMethods(); i++ { m := named.Method(i) path := append(append([]string{}, prefix...), m.Name()) g.paths[m] = path g.exported[m] = ast.IsExported(m.Name()) g.pkgscope[m] = pkgscope if s := m.Scope(); s != nil { g.assignPaths(s, path, false) } } if iface, ok := named.Underlying().(*types.Interface); ok { for i := 0; i < iface.NumExplicitMethods(); i++ { m := iface.Method(i) path := append(append([]string{}, prefix...), m.Name()) g.paths[m] = path g.exported[m] = ast.IsExported(m.Name()) g.pkgscope[m] = pkgscope if s := m.Scope(); s != nil { g.assignPaths(s, path, false) } } } }
func (w *PkgWalker) lookupNamedMethod(named *types.Named, name string) (types.Object, *types.Named) { if iface, ok := named.Underlying().(*types.Interface); ok { for i := 0; i < iface.NumMethods(); i++ { fn := iface.Method(i) if fn.Name() == name { return fn, named } } for i := 0; i < iface.NumEmbeddeds(); i++ { if obj, na := w.lookupNamedMethod(iface.Embedded(i), name); obj != nil { return obj, na } } return nil, nil } if istruct, ok := named.Underlying().(*types.Struct); ok { for i := 0; i < named.NumMethods(); i++ { fn := named.Method(i) if fn.Name() == name { return fn, named } } for i := 0; i < istruct.NumFields(); i++ { field := istruct.Field(i) if !field.Anonymous() { continue } if typ, ok := field.Type().(*types.Named); ok { if obj, na := w.lookupNamedMethod(typ, name); obj != nil { return obj, na } } } } return nil, nil }
// Type definitions are only carried through to Haxe to allow access to objects as if they were native Haxe classes. // TODO consider renaming func (l langType) TypeStart(nt *types.Named, err string) string { typName := "GoType" + l.LangName("", nt.String()) hxTyp := l.LangType(nt.Obj().Type(), false, nt.String()) ret := "" switch hxTyp { case "Object": ret += "class " + typName ret += " extends " + hxTyp + " {\n" default: ret += "abstract " + typName + "(" + hxTyp + ") from " + hxTyp + " to " + hxTyp + " {\n" } switch nt.Underlying().(type) { case *types.Struct: str := nt.Underlying().(*types.Struct) ret += "inline public function new(){ super new(" + strconv.Itoa(int(haxeStdSizes.Sizeof(nt.Obj().Type()))) + "); }\n" flds := []string{} for f := 0; f < str.NumFields(); f++ { fName := str.Field(f).Name() if len(fName) > 0 { if unicode.IsUpper(rune(fName[0])) { flds = append(flds, fName) } } } sort.Strings(flds) // make sure the fields are always in the same order in the file for _, fName := range flds { for f := 0; f < str.NumFields(); f++ { if fName == str.Field(f).Name() { haxeTyp := l.LangType(str.Field(f).Type(), false, nt.String()) fOff := fieldOffset(str, f) sfx := loadStoreSuffix(str.Field(f).Type(), true) ret += fmt.Sprintf("public var _%s(get,set):%s;\n", fName, haxeTyp) ret += fmt.Sprintf("function get__%s():%s { return get%s%d); }\n", fName, haxeTyp, sfx, fOff) ret += fmt.Sprintf("function set__%s(v:%s):%s { return set%s%d,v); }\n", fName, haxeTyp, haxeTyp, sfx, fOff) break } } } case *types.Array: ret += "inline public function new(){ super new(" + strconv.Itoa(int(haxeStdSizes.Sizeof(nt.Obj().Type()))) + "); }\n" default: // TODO not yet sure how to handle named types that are not structs ret += "inline public function new(v:" + hxTyp + ") { this = v; }\n" } meths := []string{} for m := 0; m < nt.NumMethods(); m++ { mName := nt.Method(m).Name() if len(mName) > 0 { if unicode.IsUpper(rune(mName[0])) { meths = append(meths, mName) } } } sort.Strings(meths) // make sure the methods always appear in the same order in the file for _, mName := range meths { for m := 0; m < nt.NumMethods(); m++ { meth := nt.Method(m) if mName == meth.Name() { sig := meth.Type().(*types.Signature) ret += "// " + mName + " " + sig.String() + "\n" ret += "public function _" + mName + "(" for p := 0; p < sig.Params().Len(); p++ { if p > 0 { ret += "," } ret += "_" + sig.Params().At(p).Name() + ":" + l.LangType(sig.Params().At(p).Type(), false, nt.String()) } ret += ")" switch sig.Results().Len() { case 0: ret += ":Void " case 1: ret += ":" + l.LangType(sig.Results().At(0).Type(), false, nt.String()) default: ret += ":{" for r := 0; r < sig.Results().Len(); r++ { if r > 0 { ret += "," } ret += fmt.Sprintf("r%d:%s", r, l.LangType(sig.Results().At(r).Type(), false, nt.String())) } ret += "}" } ret += "{\n\t" if sig.Results().Len() > 0 { ret += "return " } fnToCall := l.LangName( nt.Obj().Pkg().Name()+":"+sig.Recv().Type().String(), meth.Name()) ret += `Go_` + fnToCall + `.hx(this` for p := 0; p < sig.Params().Len(); p++ { ret += ", _" + sig.Params().At(p).Name() } ret += ");\n}\n" } } } l.PogoComp().WriteAsClass(typName, ret+"}\n") return "" //ret }