Пример #1
0
// FindTypes finalizes the package imports, and searches through all the
// packages for types.
func (b *Builder) FindTypes() (types.Universe, error) {
	if err := b.makePackages(); err != nil {
		return nil, err
	}

	u := types.Universe{}

	for pkgPath, pkg := range b.pkgs {
		if !b.userRequested[pkgPath] {
			// Since walkType is recursive, all types that the
			// packages they asked for depend on will be included.
			// But we don't need to include all types in all
			// *packages* they depend on.
			continue
		}

		for _, f := range b.parsed[pkgPath] {
			if strings.HasSuffix(f.name, "/doc.go") {
				if f.file.Doc != nil {
					tp := u.Package(pkgPath)
					for _, c := range f.file.Doc.List {
						tp.DocComments = append(tp.DocComments, c.Text)
					}
				}
			}
		}

		s := pkg.Scope()
		for _, n := range s.Names() {
			obj := s.Lookup(n)
			tn, ok := obj.(*tc.TypeName)
			if ok {
				t := b.walkType(u, nil, tn.Type())
				c1 := b.priorCommentLines(obj.Pos(), 1)
				t.CommentLines = c1.Text()
				if c1 == nil {
					t.SecondClosestCommentLines = b.priorCommentLines(obj.Pos(), 2).Text()
				} else {
					t.SecondClosestCommentLines = b.priorCommentLines(c1.List[0].Slash, 2).Text()
				}
			}
			tf, ok := obj.(*tc.Func)
			// We only care about functions, not concrete/abstract methods.
			if ok && tf.Type() != nil && tf.Type().(*tc.Signature).Recv() == nil {
				b.addFunction(u, nil, tf)
			}
			tv, ok := obj.(*tc.Var)
			if ok && !tv.IsField() {
				b.addVariable(u, nil, tv)
			}
		}
		for p := range b.importGraph[pkgPath] {
			u.AddImports(pkgPath, p)
		}
		u.Package(pkgPath).Name = pkg.Name()
	}
	return u, nil
}