func (check *Checker) collectObjects() {
	pkg := check.pkg
	var pkgImports = make(map[*Package]bool)
	for _, imp := range pkg.imports {
		pkgImports[imp] = true
	}
	for fileNo, file := range check.files {
		check.recordDef(file.Name, nil)
		pos, end := file.Pos(), file.End()
		if f := check.fset.File(file.Pos()); f != nil {
			pos, end = token.Pos(f.Base()), token.Pos(f.Base()+f.Size())
		}
		fileScope := NewScope(check.pkg.scope, pos, end, check.filename(fileNo))
		check.recordScope(file, fileScope)
		fileDir := dir(check.fset.Position(file.Name.Pos()).Filename)
		for _, decl := range file.Decls {
			switch d := decl.(type) {
			case *ast.BadDecl:
			case *ast.GenDecl:
				var last *ast.ValueSpec  
				for iota, spec := range d.Specs {
					switch s := spec.(type) {
					case *ast.ImportSpec:
						path, err := validatedImportPath(s.Path.Value)
						if err != nil {
							check.errorf(s.Path.Pos(), "invalid import path (%s)", err)
							continue
						}
						imp := check.importPackage(s.Path.Pos(), path, fileDir)
						if imp == nil {
							continue
						}
						if !pkgImports[imp] {
							pkgImports[imp] = true
							pkg.imports = append(pkg.imports, imp)
						}
						name := imp.name
						if s.Name != nil {
							name = s.Name.Name
							if path == "C" {
								check.errorf(s.Name.Pos(), `cannot rename import "C"`)
								continue
							}
							if name == "init" {
								check.errorf(s.Name.Pos(), "cannot declare init - must be func")
								continue
							}
						}
						obj := NewPkgName(s.Pos(), pkg, name, imp)
						if s.Name != nil {
							check.recordDef(s.Name, obj)
						} else {
							check.recordImplicit(s, obj)
						}
						if path == "C" {
							obj.used = true
						}
						if name == "." {
							for _, obj := range imp.scope.elems {
								if obj.Exported() {
									check.declare(fileScope, nil, obj, token.NoPos)
								}
							}
							check.addUnusedDotImport(fileScope, imp, s.Pos())
						} else {
							check.declare(fileScope, nil, obj, token.NoPos)
						}
					case *ast.ValueSpec:
						switch d.Tok {
						case token.CONST:
							switch {
							case s.Type != nil || len(s.Values) > 0:
								last = s
							case last == nil:
								last = new(ast.ValueSpec)  
							}
							for i, name := range s.Names {
								obj := NewConst(name.Pos(), pkg, name.Name, nil, constant.MakeInt64(int64(iota)))
								var init ast.Expr
								if i < len(last.Values) {
									init = last.Values[i]
								}
								d := &declInfo{file: fileScope, typ: last.Type, init: init}
								check.declarePkgObj(name, obj, d)
							}
							check.arityMatch(s, last)
						case token.VAR:
							lhs := make([]*Var, len(s.Names))
							var d1 *declInfo
							if len(s.Values) == 1 {
								d1 = &declInfo{file: fileScope, lhs: lhs, typ: s.Type, init: s.Values[0]}
							}
							for i, name := range s.Names {
								obj := NewVar(name.Pos(), pkg, name.Name, nil)
								lhs[i] = obj
								d := d1
								if d == nil {
									var init ast.Expr
									if i < len(s.Values) {
										init = s.Values[i]
									}
									d = &declInfo{file: fileScope, typ: s.Type, init: init}
								}
								check.declarePkgObj(name, obj, d)
							}
							check.arityMatch(s, nil)
						default:
							check.invalidAST(s.Pos(), "invalid token %s", d.Tok)
						}
					case *ast.TypeSpec:
						obj := NewTypeName(s.Name.Pos(), pkg, s.Name.Name, nil)
						check.declarePkgObj(s.Name, obj, &declInfo{file: fileScope, typeParams: s.Params, typ: s.Type, alias: s.Assign.IsValid()})
					default:
						check.invalidAST(s.Pos(), "unknown ast.Spec node %T", s)
					}
				}
			case *ast.FuncDecl:
				name := d.Name.Name
				obj := NewFunc(d.Name.Pos(), pkg, name, nil)
				if d.Recv == nil {
					if name == "init" {
						obj.parent = pkg.scope
						check.recordDef(d.Name, obj)
						if d.Body == nil {
							check.softErrorf(obj.pos, "missing function body")
						}
					} else {
						check.declare(pkg.scope, d.Name, obj, token.NoPos)
					}
				} else {
					check.recordDef(d.Name, obj)
					if list := d.Recv.List; len(list) > 0 {
						typ := unparen(list[0].Type)
						if ptr, ok := typ.(*ast.StarExpr); ok {
							typ = unparen(ptr.X)
						}
						if call, ok := typ.(*ast.CallExpr); ok {  
							typ = call.Fun
						}
						if base, _ := typ.(*ast.Ident); base != nil && base.Name != "_" {
							check.assocMethod(base.Name, obj)
						}
					}
				}
				info := &declInfo{file: fileScope, fdecl: d}
				check.objMap[obj] = info
				obj.setOrder(uint32(len(check.objMap)))
			default:
				check.invalidAST(d.Pos(), "unknown ast.Decl node %T", d)
			}
		}
	}
	for _, scope := range check.pkg.scope.children   {
		for _, obj := range scope.elems {
			if alt := pkg.scope.Lookup(obj.Name()); alt != nil {
				if pkg, ok := obj.(*PkgName); ok {
					check.errorf(alt.Pos(), "%s already declared through import of %s", alt.Name(), pkg.Imported())
					check.reportAltDecl(pkg)
				} else {
					check.errorf(alt.Pos(), "%s already declared through dot-import of %s", alt.Name(), obj.Pkg())
					check.reportAltDecl(obj)
				}
			}
		}
	}
}
collectObjects collects all file and package objects and inserts them
into their respective scopes. It also performs imports and associates
methods with receiver base type names.
package types
import (
	"fmt"
	"github.com/faiface/generics/go/ast"
	"github.com/faiface/generics/go/constant"
	"github.com/faiface/generics/go/token"
	"strconv"
	"strings"
	"unicode"
)
type declInfo struct {
	file       *Scope            
	lhs        []*Var            
	typeParams []*ast.TypeParam  
	typ        ast.Expr          
	init       ast.Expr          
	fdecl      *ast.FuncDecl     
	alias      bool              
	deps objSet  
}
type objSet map[Object]bool
func (d *declInfo) hasInitializer() bool {
	return d.init != nil || d.fdecl != nil && d.fdecl.Body != nil
}
func (d *declInfo) addDep(obj Object) {
	m := d.deps
	if m == nil {
		m = make(objSet)
		d.deps = m
	}
	m[obj] = true
}
func (check *Checker) arityMatch(s, init *ast.ValueSpec) {
	l := len(s.Names)
	r := len(s.Values)
	if init != nil {
		r = len(init.Values)
	}
	switch {
	case init == nil && r == 0:
		if s.Type == nil {
			check.errorf(s.Pos(), "missing type or init expr")
		}
	case l < r:
		if l < len(s.Values) {
			n := s.Values[l]
			check.errorf(n.Pos(), "extra init expr %s", n)
		} else {
			check.errorf(s.Pos(), "extra init expr at %s", check.fset.Position(init.Pos()))
		}
	case l > r && (init != nil || r != 1):
		n := s.Names[r]
		check.errorf(n.Pos(), "missing init expr for %s", n)
	}
}
func validatedImportPath(path string) (string, error) {
	s, err := strconv.Unquote(path)
	if err != nil {
		return "", err
	}
	if s == "" {
		return "", fmt.Errorf("empty string")
	}
	const illegalChars = `!"#$%&'()*,:;<=>?[\]^{|}` + "`\uFFFD"
	for _, r := range s {
		if !unicode.IsGraphic(r) || unicode.IsSpace(r) || strings.ContainsRune(illegalChars, r) {
			return s, fmt.Errorf("invalid character %#U", r)
		}
	}
	return s, nil
}
// declarePkgObj declares obj in the package scope, records its ident -> obj mapping,
// and updates check.objMap. The object must not be a function or method.
func (check *Checker) declarePkgObj(ident *ast.Ident, obj Object, d *declInfo) {
	assert(ident.Name == obj.Name())
	// spec: "A package-scope or file-scope identifier with name init
	if ident.Name == "init" {
		check.errorf(ident.Pos(), "cannot declare init - must be func")
		return
	}
	if ident.Name == "main" && check.pkg.name == "main" {
		check.errorf(ident.Pos(), "cannot declare main - must be func")
		return
	}
	check.declare(check.pkg.scope, ident, obj, token.NoPos)
	check.objMap[obj] = d
	obj.setOrder(uint32(len(check.objMap)))
}
func (check *Checker) filename(fileNo int) string {
	file := check.files[fileNo]
	if pos := file.Pos(); pos.IsValid() {
		return check.fset.File(pos).Name()
	}
	return fmt.Sprintf("file[%d]", fileNo)
}
func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
	key := importKey{path, dir}
	imp := check.impMap[key]
	if imp != nil {
		return imp
	}
	if path == "C" && check.conf.FakeImportC {
		imp = NewPackage("C", "C")
		imp.fake = true
	} else {
		var err error
		if importer := check.conf.Importer; importer == nil {
			err = fmt.Errorf("Config.Importer not installed")
		} else if importerFrom, ok := importer.(ImporterFrom); ok {
			imp, err = importerFrom.ImportFrom(path, dir, 0)
			if imp == nil && err == nil {
				err = fmt.Errorf("Config.Importer.ImportFrom(%s, %s, 0) returned nil but no error", path, dir)
			}
		} else {
			imp, err = importer.Import(path)
			if imp == nil && err == nil {
				err = fmt.Errorf("Config.Importer.Import(%s) returned nil but no error", path)
			}
		}
		if err == nil && imp != nil && (imp.name == "_" || imp.name == "") {
			err = fmt.Errorf("invalid package name: %q", imp.name)
			imp = nil  
		}
		if err != nil {
			check.errorf(pos, "could not import %s (%s)", path, err)
			if imp == nil {
				name := path
				if i := len(name); i > 0 && name[i-1] == '/' {
					name = name[:i-1]
				}
				if i := strings.LastIndex(name, "/"); i >= 0 {
					name = name[i+1:]
				}
				imp = NewPackage(path, name)
			}
			imp.fake = true  
		}
	}
	if imp.complete || imp.fake {
		check.impMap[key] = imp
		return imp
	}
	return nil
}