Exemplo n.º 1
0
func (c *converter) convertNamed(v *gotypes.Named) *types.Named {
	if v == nil {
		return nil
	}
	if v, ok := c.converted[v]; ok {
		return v.(*types.Named)
	}
	if gotypes.Universe.Lookup("error").(*gotypes.TypeName).Type().(*gotypes.Named) == v {
		return types.Universe.Lookup("error").(*types.TypeName).Type().(*types.Named)
	}
	typeName := c.convertTypeName(v.Obj())
	ret, ok := typeName.Type().(*types.Named)
	if !ok {
		ret = types.NewNamed(nil, nil, nil)
	}
	c.converted[v] = ret
	for i := 0; i < v.NumMethods(); i++ {
		ret.AddMethod(c.convertFunc(v.Method(i)))
	}
	ret.SetUnderlying(c.convertType(v.Underlying()))
	return ret
}
Exemplo n.º 2
0
func (p *exporter) assocMethods(named *types.Named) {
	// Sort methods (for determinism).
	var methods []*types.Func
	for i := 0; i < named.NumMethods(); i++ {
		methods = append(methods, named.Method(i))
	}
	sort.Sort(methodsByName(methods))

	p.int(len(methods))

	if trace && methods != nil {
		p.tracef("associated methods {>\n")
	}

	for i, m := range methods {
		if trace && i > 0 {
			p.tracef("\n")
		}

		p.pos(m)
		name := m.Name()
		p.string(name)
		if !exported(name) {
			p.pkg(m.Pkg(), false)
		}

		sig := m.Type().(*types.Signature)
		p.paramList(types.NewTuple(sig.Recv()), false)
		p.paramList(sig.Params(), sig.Variadic())
		p.paramList(sig.Results(), false)
		p.int(0) // dummy value for go:nointerface pragma - ignored by importer
	}

	if trace && methods != nil {
		p.tracef("<\n} ")
	}
}