Ejemplo n.º 1
0
// For a given struct type and (promoted) field Id, findEmbeddedField
// returns the path of implicit anonymous field selections, and the
// field index of the explicit (=outermost) selection.
//
// TODO(gri): if go/types/operand.go's lookupFieldBreadthFirst were to
// record (e.g. call a client-provided callback) the implicit field
// selection path discovered for a particular ast.SelectorExpr, we could
// eliminate this function.
//
func findPromotedField(st *types.Struct, id Id) (*anonFieldPath, int) {
	// visited records the types that have been searched already.
	// Invariant: keys are all *types.Named.
	// (types.Type is not a sound map key in general.)
	visited := make(map[types.Type]bool)

	var list, next []*anonFieldPath
	i := 0
	st.ForEachField(func(f *types.Field) {
		if f.IsAnonymous {
			list = append(list, &anonFieldPath{nil, i, f})
		}
		i++
	})

	// Search the current level if there is any work to do and collect
	// embedded types of the next lower level in the next list.
	for {
		// look for name in all types at this level
		for _, node := range list {
			typ := node.field.Type.Deref().(*types.Named)
			if visited[typ] {
				continue
			}
			visited[typ] = true

			switch typ := typ.Underlying().(type) {
			case *types.Struct:
				for i, n := 0, typ.NumFields(); i < n; i++ {
					f := typ.Field(i)
					if MakeId(f.Name, f.Pkg) == id {
						return node, i
					}
				}
				i := 0
				typ.ForEachField(func(f *types.Field) {
					if f.IsAnonymous {
						next = append(next, &anonFieldPath{node, i, f})
					}
					i++
				})
			}
		}

		if len(next) == 0 {
			panic("field not found: " + id.String())
		}

		// No match so far.
		list, next = next, list[:0] // reuse arrays
	}
	panic("unreachable")
}