Beispiel #1
0
func newPkgStruct(s *ast.Struct) *pkgStruct {
	deps := listStructDeps(s)
	t := types.NewStruct(s.Name.Lit)

	return &pkgStruct{
		name: s.Name,
		ast:  s,
		deps: deps,
		t:    t,
		pt:   types.NewPointer(t),
	}
}
Beispiel #2
0
func buildMember(b *builder, m *tast.MemberExpr) *ref {
	obj := b.buildExpr(m.Expr)
	if !obj.IsSingle() {
		panic("not single")
	}

	t := obj.Type()
	if pkg, ok := t.(*types.Pkg); ok {
		return buildPkgSym(b, m, pkg)
	}

	pt := types.PointerOf(t)
	var tstruct *types.Struct
	if pt != nil {
		tstruct = pt.(*types.Struct)
	} else {
		tstruct = t.(*types.Struct)
	}

	addr := b.newPtr()
	if pt != nil {
		b.b.Assign(addr, obj.IR())
		if obj != b.this {
			nilPointerPanic(b, addr)
		}
	} else {
		b.b.Arith(addr, nil, "&", obj.IR())
	}

	sym := m.Symbol
	if sym.Type == tast.SymField {
		return buildField(b, addr, sym.Obj.(*types.Field))
	} else if sym.Type == tast.SymFunc {
		recv := newRef(types.NewPointer(tstruct), addr)
		method := sym.Obj.(*objFunc)
		ft := method.Type().(*types.Func)
		return newRecvRef(ft, recv, method.IR())
	}

	panic("bug")
}