1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
27 "cmd/go/internal/base"
28 "cmd/go/internal/cache"
30 "cmd/go/internal/fsys"
31 "cmd/go/internal/imports"
36 // enabled is used to flag off the behavior of the module index on tip.
37 // It will be removed before the release.
38 // TODO(matloob): Remove enabled once we have more confidence on the
40 var enabled = godebug.New("goindex").Value() != "0"
42 // Module represents and encoded module index file. It is used to
43 // do the equivalent of build.Import of packages in the module and answer other
44 // questions based on the index file's data.
48 n int // number of packages
51 // moduleHash returns an ActionID corresponding to the state of the module
52 // located at filesystem path modroot.
53 func moduleHash(modroot string, ismodcache bool) (cache.ActionID, error) {
54 // We expect modules stored within the module cache to be checksummed and
55 // immutable, and we expect released modules within GOROOT to change only
56 // infrequently (when the Go version changes).
58 // The contents of this module may change over time. We don't want to pay
59 // the cost to detect changes and re-index whenever they occur, so just
60 // don't index it at all.
62 // Note that this is true even for modules in GOROOT/src: non-release builds
63 // of the Go toolchain may have arbitrary development changes on top of the
64 // commit reported by runtime.Version, or could be completly artificial due
65 // to lacking a `git` binary (like "devel gomote.XXXXX", as synthesized by
66 // "gomote push" as of 2022-06-15). (Release builds shouldn't have
67 // modifications, but we don't want to use a behavior for releases that we
68 // haven't tested during development.)
69 return cache.ActionID{}, ErrNotIndexed
72 h := cache.NewHash("moduleIndex")
73 // TODO(bcmills): Since modules in the index are checksummed, we could
74 // probably improve the cache hit rate by keying off of the module
75 // path@version (perhaps including the checksum?) instead of the module root
77 fmt.Fprintf(h, "module index %s %s %v\n", runtime.Version(), indexVersion, modroot)
81 const modTimeCutoff = 2 * time.Second
83 // dirHash returns an ActionID corresponding to the state of the package
84 // located at filesystem path pkgdir.
85 func dirHash(modroot, pkgdir string) (cache.ActionID, error) {
86 h := cache.NewHash("moduleIndex")
87 fmt.Fprintf(h, "modroot %s\n", modroot)
88 fmt.Fprintf(h, "package %s %s %v\n", runtime.Version(), indexVersion, pkgdir)
89 entries, err := fsys.ReadDir(pkgdir)
91 // pkgdir might not be a directory. give up on hashing.
92 return cache.ActionID{}, ErrNotIndexed
94 cutoff := time.Now().Add(-modTimeCutoff)
95 for _, info := range entries {
100 if !info.Mode().IsRegular() {
101 return cache.ActionID{}, ErrNotIndexed
103 // To avoid problems for very recent files where a new
104 // write might not change the mtime due to file system
105 // mtime precision, reject caching if a file was read that
106 // is less than modTimeCutoff old.
108 // This is the same strategy used for hashing test inputs.
109 // See hashOpen in cmd/go/internal/test/test.go for the
110 // corresponding code.
111 if info.ModTime().After(cutoff) {
112 return cache.ActionID{}, ErrNotIndexed
115 fmt.Fprintf(h, "file %v %v %v\n", info.Name(), info.ModTime(), info.Size())
120 var modrootCache par.Cache
122 var ErrNotIndexed = errors.New("not in module index")
125 errDisabled = fmt.Errorf("%w: module indexing disabled", ErrNotIndexed)
126 errNotFromModuleCache = fmt.Errorf("%w: not from module cache", ErrNotIndexed)
129 // GetPackage returns the IndexPackage for the package at the given path.
130 // It will return ErrNotIndexed if the directory should be read without
131 // using the index, for instance because the index is disabled, or the packgae
132 // is not in a module.
133 func GetPackage(modroot, pkgdir string) (*IndexPackage, error) {
134 mi, err := GetModule(modroot)
136 return mi.Package(relPath(pkgdir, modroot)), nil
138 if !errors.Is(err, errNotFromModuleCache) {
141 if cfg.BuildContext.Compiler == "gccgo" && str.HasPathPrefix(modroot, cfg.GOROOTsrc) {
142 return nil, err // gccgo has no sources for GOROOT packages.
144 return openIndexPackage(modroot, pkgdir)
147 // GetModule returns the Module for the given modroot.
148 // It will return ErrNotIndexed if the directory should be read without
149 // using the index, for instance because the index is disabled, or the packgae
150 // is not in a module.
151 func GetModule(modroot string) (*Module, error) {
152 if !enabled || cache.DefaultDir() == "off" {
153 return nil, errDisabled
156 panic("modindex.GetPackage called with empty modroot")
158 if cfg.BuildMod == "vendor" {
159 // Even if the main module is in the module cache,
160 // its vendored dependencies are not loaded from their
161 // usual cached locations.
162 return nil, errNotFromModuleCache
164 modroot = filepath.Clean(modroot)
165 if !str.HasFilePathPrefix(modroot, cfg.GOMODCACHE) {
166 return nil, errNotFromModuleCache
168 return openIndexModule(modroot, true)
173 // openIndexModule returns the module index for modPath.
174 // It will return ErrNotIndexed if the module can not be read
175 // using the index because it contains symlinks.
176 func openIndexModule(modroot string, ismodcache bool) (*Module, error) {
181 r := mcache.Do(modroot, func() any {
182 fsys.Trace("openIndexModule", modroot)
183 id, err := moduleHash(modroot, ismodcache)
185 return result{nil, err}
187 data, _, err := cache.Default().GetMmap(id)
189 // Couldn't read from modindex. Assume we couldn't read from
190 // the index because the module hasn't been indexed yet.
191 data, err = indexModule(modroot)
193 return result{nil, err}
195 if err = cache.Default().PutBytes(id, data); err != nil {
196 return result{nil, err}
199 mi, err := fromBytes(modroot, data)
201 return result{nil, err}
203 return result{mi, nil}
210 func openIndexPackage(modroot, pkgdir string) (*IndexPackage, error) {
215 r := pcache.Do([2]string{modroot, pkgdir}, func() any {
216 fsys.Trace("openIndexPackage", pkgdir)
217 id, err := dirHash(modroot, pkgdir)
219 return result{nil, err}
221 data, _, err := cache.Default().GetMmap(id)
223 // Couldn't read from index. Assume we couldn't read from
224 // the index because the package hasn't been indexed yet.
225 data = indexPackage(modroot, pkgdir)
226 if err = cache.Default().PutBytes(id, data); err != nil {
227 return result{nil, err}
230 pkg, err := packageFromBytes(modroot, data)
232 return result{nil, err}
234 return result{pkg, nil}
239 var errCorrupt = errors.New("corrupt index")
241 // protect marks the start of a large section of code that accesses the index.
242 // It should be used as:
244 // defer unprotect(protect, &err)
246 // It should not be used for trivial accesses which would be
247 // dwarfed by the overhead of the defer.
248 func protect() bool {
249 return debug.SetPanicOnFault(true)
254 // unprotect marks the end of a large section of code that accesses the index.
255 // It should be used as:
257 // defer unprotect(protect, &err)
259 // end looks for panics due to errCorrupt or bad mmap accesses.
260 // When it finds them, it adds explanatory text, consumes the panic, and sets *errp instead.
261 // If errp is nil, end adds the explanatory text but then calls base.Fatalf.
262 func unprotect(old bool, errp *error) {
263 // SetPanicOnFault's errors _may_ satisfy this interface. Even though it's not guaranteed
264 // that all its errors satisfy this interface, we'll only check for these errors so that
265 // we don't suppress panics that could have been produced from other sources.
266 type addrer interface {
270 debug.SetPanicOnFault(old)
272 if e := recover(); e != nil {
273 if _, ok := e.(addrer); ok || e == errCorrupt {
274 // This panic was almost certainly caused by SetPanicOnFault or our panic(errCorrupt).
275 err := fmt.Errorf("error reading module index: %v", e)
283 base.Fatalf("%v", err)
285 // The panic was likely not caused by SetPanicOnFault.
290 // fromBytes returns a *Module given the encoded representation.
291 func fromBytes(moddir string, data []byte) (m *Module, err error) {
293 panic("use of index")
296 defer unprotect(protect(), &err)
298 if !bytes.HasPrefix(data, []byte(indexVersion+"\n")) {
299 return nil, errCorrupt
302 const hdr = len(indexVersion + "\n")
303 d := &decoder{data: data}
305 if str < hdr+8 || len(d.data) < str {
306 return nil, errCorrupt
308 d.data, d.str = data[:str], d.data[str:]
309 // Check that string table looks valid.
310 // First string is empty string (length 0),
311 // and we leave a marker byte 0xFF at the end
312 // just to make sure that the file is not truncated.
313 if len(d.str) == 0 || d.str[0] != 0 || d.str[len(d.str)-1] != 0xFF {
314 return nil, errCorrupt
317 n := d.intAt(hdr + 4)
318 if n < 0 || n > (len(d.data)-8)/8 {
319 return nil, errCorrupt
330 // packageFromBytes returns a *IndexPackage given the encoded representation.
331 func packageFromBytes(modroot string, data []byte) (p *IndexPackage, err error) {
332 m, err := fromBytes(modroot, data)
337 return nil, fmt.Errorf("corrupt single-package index")
342 // pkgDir returns the dir string of the i'th package in the index.
343 func (m *Module) pkgDir(i int) string {
344 if i < 0 || i >= m.n {
347 return m.d.stringAt(12 + 8 + 8*i)
350 // pkgOff returns the offset of the data for the i'th package in the index.
351 func (m *Module) pkgOff(i int) int {
352 if i < 0 || i >= m.n {
355 return m.d.intAt(12 + 8 + 8*i + 4)
358 // Walk calls f for each package in the index, passing the path to that package relative to the module root.
359 func (m *Module) Walk(f func(path string)) {
360 defer unprotect(protect(), nil)
361 for i := 0; i < m.n; i++ {
366 // relPath returns the path relative to the module's root.
367 func relPath(path, modroot string) string {
368 return str.TrimFilePathPrefix(filepath.Clean(path), filepath.Clean(modroot))
371 var installgorootAll = godebug.New("installgoroot").Value() == "all"
373 // Import is the equivalent of build.Import given the information in Module.
374 func (rp *IndexPackage) Import(bctxt build.Context, mode build.ImportMode) (p *build.Package, err error) {
375 defer unprotect(protect(), &err)
377 ctxt := (*Context)(&bctxt)
382 p.Dir = filepath.Join(rp.modroot, rp.dir)
385 switch ctxt.Compiler {
388 // Save error for end of function.
389 pkgerr = fmt.Errorf("import %q: unknown compiler %q", p.Dir, ctxt.Compiler)
393 return p, fmt.Errorf("import %q: import of unknown directory", p.Dir)
397 inTestdata := func(sub string) bool {
398 return strings.Contains(sub, "/testdata/") || strings.HasSuffix(sub, "/testdata") || str.HasPathPrefix(sub, "testdata")
401 if !inTestdata(rp.dir) {
402 // In build.go, p.Root should only be set in the non-local-import case, or in
403 // GOROOT or GOPATH. Since module mode only calls Import with path set to "."
404 // and the module index doesn't apply outside modules, the GOROOT case is
405 // the only case where p.Root needs to be set.
406 if ctxt.GOROOT != "" && str.HasFilePathPrefix(p.Dir, cfg.GOROOTsrc) && p.Dir != cfg.GOROOTsrc {
409 modprefix := str.TrimFilePathPrefix(rp.modroot, cfg.GOROOTsrc)
410 p.ImportPath = rp.dir
412 p.ImportPath = filepath.Join(modprefix, p.ImportPath)
415 // Set GOROOT-specific fields (sometimes for modules in a GOPATH directory).
416 // The fields set below (SrcRoot, PkgRoot, BinDir, PkgTargetRoot, and PkgObj)
417 // are only set in build.Import if p.Root != "".
418 var pkgtargetroot string
420 if ctxt.InstallSuffix != "" {
421 suffix = "_" + ctxt.InstallSuffix
423 switch ctxt.Compiler {
425 pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
426 dir, elem := path.Split(p.ImportPath)
427 pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
429 pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
430 pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
432 p.SrcRoot = ctxt.joinPath(p.Root, "src")
433 p.PkgRoot = ctxt.joinPath(p.Root, "pkg")
434 p.BinDir = ctxt.joinPath(p.Root, "bin")
436 // Always set PkgTargetRoot. It might be used when building in shared
438 p.PkgTargetRoot = ctxt.joinPath(p.Root, pkgtargetroot)
440 // Set the install target if applicable.
441 if !p.Goroot || (installgorootAll && p.ImportPath != "unsafe" && p.ImportPath != "builtin") {
442 p.PkgObj = ctxt.joinPath(p.Root, pkga)
449 if errors.Is(rp.error, errCannotFindPackage) && ctxt.Compiler == "gccgo" && p.Goroot {
455 if mode&build.FindOnly != 0 {
459 // We need to do a second round of bad file processing.
461 badGoFiles := make(map[string]bool)
462 badGoFile := func(name string, err error) {
463 if badGoError == nil {
466 if !badGoFiles[name] {
467 p.InvalidGoFiles = append(p.InvalidGoFiles, name)
468 badGoFiles[name] = true
472 var Sfiles []string // files with ".S"(capital S)/.sx(capital s equivalent for case insensitive filesystems)
474 embedPos := make(map[string][]token.Position)
475 testEmbedPos := make(map[string][]token.Position)
476 xTestEmbedPos := make(map[string][]token.Position)
477 importPos := make(map[string][]token.Position)
478 testImportPos := make(map[string][]token.Position)
479 xTestImportPos := make(map[string][]token.Position)
480 allTags := make(map[string]bool)
481 for _, tf := range rp.sourceFiles {
483 // Check errors for go files and call badGoFiles to put them in
484 // InvalidGoFiles if they do have an error.
485 if strings.HasSuffix(name, ".go") {
486 if error := tf.error(); error != "" {
487 badGoFile(name, errors.New(tf.error()))
489 } else if parseError := tf.parseError(); parseError != "" {
490 badGoFile(name, parseErrorFromString(tf.parseError()))
491 // Fall through: we still want to list files with parse errors.
495 var shouldBuild = true
496 if !ctxt.goodOSArchFile(name, allTags) && !ctxt.UseAllFiles {
498 } else if goBuildConstraint := tf.goBuildConstraint(); goBuildConstraint != "" {
499 x, err := constraint.Parse(goBuildConstraint)
501 return p, fmt.Errorf("%s: parsing //go:build line: %v", name, err)
503 shouldBuild = ctxt.eval(x, allTags)
504 } else if plusBuildConstraints := tf.plusBuildConstraints(); len(plusBuildConstraints) > 0 {
505 for _, text := range plusBuildConstraints {
506 if x, err := constraint.Parse(text); err == nil {
507 if !ctxt.eval(x, allTags) {
515 if !shouldBuild || tf.ignoreFile() {
517 p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
518 } else if fileListForExt((*Package)(p), ext) != nil {
519 p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, name)
524 // Going to save the file. For non-Go files, can stop here.
529 // special case for cgo, handled at end
530 Sfiles = append(Sfiles, name)
533 if list := fileListForExt((*Package)(p), ext); list != nil {
534 *list = append(*list, name)
540 if pkg == "documentation" {
541 p.IgnoredGoFiles = append(p.IgnoredGoFiles, name)
544 isTest := strings.HasSuffix(name, "_test.go")
546 if isTest && strings.HasSuffix(tf.pkgName(), "_test") && p.Name != tf.pkgName() {
548 pkg = pkg[:len(pkg)-len("_test")]
551 if !isTest && tf.binaryOnly() {
558 } else if pkg != p.Name {
559 // TODO(#45999): The choice of p.Name is arbitrary based on file iteration
560 // order. Instead of resolving p.Name arbitrarily, we should clear out the
561 // existing Name and mark the existing files as also invalid.
562 badGoFile(name, &MultiplePackageError{
564 Packages: []string{p.Name, pkg},
565 Files: []string{firstFile, name},
568 // Grab the first package comment as docs, provided it is not from a test file.
569 if p.Doc == "" && !isTest && !isXTest {
570 if synopsis := tf.synopsis(); synopsis != "" {
575 // Record Imports and information about cgo.
577 imports := tf.imports()
578 for _, imp := range imports {
581 badGoFile(name, fmt.Errorf("use of cgo in test %s not supported", name))
587 if directives := tf.cgoDirectives(); directives != "" {
588 if err := ctxt.saveCgo(name, (*Package)(p), directives); err != nil {
593 var fileList *[]string
594 var importMap, embedMap map[string][]token.Position
597 allTags["cgo"] = true
599 fileList = &p.CgoFiles
600 importMap = importPos
603 // Ignore Imports and Embeds from cgo files if cgo is disabled.
604 fileList = &p.IgnoredGoFiles
607 fileList = &p.XTestGoFiles
608 importMap = xTestImportPos
609 embedMap = xTestEmbedPos
611 fileList = &p.TestGoFiles
612 importMap = testImportPos
613 embedMap = testEmbedPos
615 fileList = &p.GoFiles
616 importMap = importPos
619 *fileList = append(*fileList, name)
620 if importMap != nil {
621 for _, imp := range imports {
622 importMap[imp.path] = append(importMap[imp.path], imp.position)
626 for _, e := range tf.embeds() {
627 embedMap[e.pattern] = append(embedMap[e.pattern], e.position)
632 p.EmbedPatterns, p.EmbedPatternPos = cleanDecls(embedPos)
633 p.TestEmbedPatterns, p.TestEmbedPatternPos = cleanDecls(testEmbedPos)
634 p.XTestEmbedPatterns, p.XTestEmbedPatternPos = cleanDecls(xTestEmbedPos)
636 p.Imports, p.ImportPos = cleanDecls(importPos)
637 p.TestImports, p.TestImportPos = cleanDecls(testImportPos)
638 p.XTestImports, p.XTestImportPos = cleanDecls(xTestImportPos)
640 for tag := range allTags {
641 p.AllTags = append(p.AllTags, tag)
643 sort.Strings(p.AllTags)
645 if len(p.CgoFiles) > 0 {
646 p.SFiles = append(p.SFiles, Sfiles...)
647 sort.Strings(p.SFiles)
649 p.IgnoredOtherFiles = append(p.IgnoredOtherFiles, Sfiles...)
650 sort.Strings(p.IgnoredOtherFiles)
653 if badGoError != nil {
656 if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
657 return p, &build.NoGoError{Dir: p.Dir}
662 // IsStandardPackage reports whether path is a standard package
663 // for the goroot and compiler using the module index if possible,
664 // and otherwise falling back to internal/goroot.IsStandardPackage
665 func IsStandardPackage(goroot_, compiler, path string) bool {
666 if !enabled || compiler != "gc" {
667 return goroot.IsStandardPackage(goroot_, compiler, path)
670 reldir := filepath.FromSlash(path) // relative dir path in module index for package
671 modroot := filepath.Join(goroot_, "src")
672 if str.HasFilePathPrefix(reldir, "cmd") {
673 reldir = str.TrimFilePathPrefix(reldir, "cmd")
674 modroot = filepath.Join(modroot, "cmd")
676 if _, err := GetPackage(modroot, filepath.Join(modroot, reldir)); err == nil {
677 // Note that goroot.IsStandardPackage doesn't check that the directory
678 // actually contains any go files-- merely that it exists. GetPackage
679 // returning a nil error is enough for us to know the directory exists.
681 } else if errors.Is(err, ErrNotIndexed) {
682 // Fall back because package isn't indexable. (Probably because
683 // a file was modified recently)
684 return goroot.IsStandardPackage(goroot_, compiler, path)
689 // IsDirWithGoFiles is the equivalent of fsys.IsDirWithGoFiles using the information in the index.
690 func (rp *IndexPackage) IsDirWithGoFiles() (_ bool, err error) {
692 if e := recover(); e != nil {
693 err = fmt.Errorf("error reading module index: %v", e)
696 for _, sf := range rp.sourceFiles {
697 if strings.HasSuffix(sf.name(), ".go") {
704 // ScanDir implements imports.ScanDir using the information in the index.
705 func (rp *IndexPackage) ScanDir(tags map[string]bool) (sortedImports []string, sortedTestImports []string, err error) {
706 // TODO(matloob) dir should eventually be relative to indexed directory
707 // TODO(matloob): skip reading raw package and jump straight to data we need?
710 if e := recover(); e != nil {
711 err = fmt.Errorf("error reading module index: %v", e)
715 imports_ := make(map[string]bool)
716 testImports := make(map[string]bool)
720 for _, sf := range rp.sourceFiles {
722 if strings.HasPrefix(name, "_") || strings.HasPrefix(name, ".") || !strings.HasSuffix(name, ".go") || !imports.MatchFile(name, tags) {
726 // The following section exists for backwards compatibility reasons:
727 // scanDir ignores files with import "C" when collecting the list
728 // of imports unless the "cgo" tag is provided. The following comment
729 // is copied from the original.
731 // import "C" is implicit requirement of cgo tag.
732 // When listing files on the command line (explicitFiles=true)
733 // we do not apply build tag filtering but we still do apply
734 // cgo filtering, so no explicitFiles check here.
735 // Why? Because we always have, and it's not worth breaking
736 // that behavior now.
737 imps := sf.imports() // TODO(matloob): directly read import paths to avoid the extra strings?
738 for _, imp := range imps {
739 if imp.path == "C" && !tags["cgo"] && !tags["*"] {
744 if !shouldBuild(sf, tags) {
749 if strings.HasSuffix(name, "_test.go") {
752 for _, p := range imps {
757 return nil, nil, imports.ErrNoGo
759 return keys(imports_), keys(testImports), nil
762 func keys(m map[string]bool) []string {
763 list := make([]string, 0, len(m))
765 list = append(list, k)
771 // implements imports.ShouldBuild in terms of an index sourcefile.
772 func shouldBuild(sf *sourceFile, tags map[string]bool) bool {
773 if goBuildConstraint := sf.goBuildConstraint(); goBuildConstraint != "" {
774 x, err := constraint.Parse(goBuildConstraint)
778 return imports.Eval(x, tags, true)
781 plusBuildConstraints := sf.plusBuildConstraints()
782 for _, text := range plusBuildConstraints {
783 if x, err := constraint.Parse(text); err == nil {
784 if !imports.Eval(x, tags, true) {
793 // IndexPackage holds the information needed to access information in the
794 // index needed to load a package in a specific directory.
795 type IndexPackage struct {
797 dir string // directory of the package relative to the modroot
802 sourceFiles []*sourceFile
805 var errCannotFindPackage = errors.New("cannot find package")
807 // Package and returns finds the package with the given path (relative to the module root).
808 // If the package does not exist, Package returns an IndexPackage that will return an
809 // appropriate error from its methods.
810 func (m *Module) Package(path string) *IndexPackage {
811 defer unprotect(protect(), nil)
813 i, ok := sort.Find(m.n, func(i int) int {
814 return strings.Compare(path, m.pkgDir(i))
817 return &IndexPackage{error: fmt.Errorf("%w %q in:\n\t%s", errCannotFindPackage, path, filepath.Join(m.modroot, path))}
822 // pkgAt returns the i'th IndexPackage in m.
823 func (m *Module) pkg(i int) *IndexPackage {
824 r := m.d.readAt(m.pkgOff(i))
825 p := new(IndexPackage)
826 if errstr := r.string(); errstr != "" {
827 p.error = errors.New(errstr)
830 p.sourceFiles = make([]*sourceFile, r.int())
831 for i := range p.sourceFiles {
832 p.sourceFiles[i] = &sourceFile{
837 p.modroot = m.modroot
841 // sourceFile represents the information of a given source file in the module index.
842 type sourceFile struct {
843 d *decoder // encoding of this source file
844 pos int // start of sourceFile encoding in d
845 onceReadImports sync.Once
846 savedImports []rawImport // saved imports so that they're only read once
849 // Offsets for fields in the sourceFile.
851 sourceFileError = 4 * iota
858 sourceFileCgoDirectives
859 sourceFileGoBuildConstraint
860 sourceFileNumPlusBuildConstraints
863 func (sf *sourceFile) error() string {
864 return sf.d.stringAt(sf.pos + sourceFileError)
866 func (sf *sourceFile) parseError() string {
867 return sf.d.stringAt(sf.pos + sourceFileParseError)
869 func (sf *sourceFile) synopsis() string {
870 return sf.d.stringAt(sf.pos + sourceFileSynopsis)
872 func (sf *sourceFile) name() string {
873 return sf.d.stringAt(sf.pos + sourceFileName)
875 func (sf *sourceFile) pkgName() string {
876 return sf.d.stringAt(sf.pos + sourceFilePkgName)
878 func (sf *sourceFile) ignoreFile() bool {
879 return sf.d.boolAt(sf.pos + sourceFileIgnoreFile)
881 func (sf *sourceFile) binaryOnly() bool {
882 return sf.d.boolAt(sf.pos + sourceFileBinaryOnly)
884 func (sf *sourceFile) cgoDirectives() string {
885 return sf.d.stringAt(sf.pos + sourceFileCgoDirectives)
887 func (sf *sourceFile) goBuildConstraint() string {
888 return sf.d.stringAt(sf.pos + sourceFileGoBuildConstraint)
891 func (sf *sourceFile) plusBuildConstraints() []string {
892 pos := sf.pos + sourceFileNumPlusBuildConstraints
895 ret := make([]string, n)
896 for i := 0; i < n; i++ {
897 ret[i] = sf.d.stringAt(pos)
903 func (sf *sourceFile) importsOffset() int {
904 pos := sf.pos + sourceFileNumPlusBuildConstraints
906 // each build constraint is 1 uint32
910 func (sf *sourceFile) embedsOffset() int {
911 pos := sf.importsOffset()
913 // each import is 5 uint32s (string + tokpos)
914 return pos + 4 + n*(4*5)
917 func (sf *sourceFile) imports() []rawImport {
918 sf.onceReadImports.Do(func() {
919 importsOffset := sf.importsOffset()
920 r := sf.d.readAt(importsOffset)
921 numImports := r.int()
922 ret := make([]rawImport, numImports)
923 for i := 0; i < numImports; i++ {
924 ret[i] = rawImport{r.string(), r.tokpos()}
926 sf.savedImports = ret
928 return sf.savedImports
931 func (sf *sourceFile) embeds() []embed {
932 embedsOffset := sf.embedsOffset()
933 r := sf.d.readAt(embedsOffset)
935 ret := make([]embed, numEmbeds)
937 ret[i] = embed{r.string(), r.tokpos()}
942 func asString(b []byte) string {
943 return unsafe.String(unsafe.SliceData(b), len(b))
946 // A decoder helps decode the index format.
947 type decoder struct {
948 data []byte // data after header
949 str []byte // string table
952 // intAt returns the int at the given offset in d.data.
953 func (d *decoder) intAt(off int) int {
954 if off < 0 || len(d.data)-off < 4 {
957 i := binary.LittleEndian.Uint32(d.data[off : off+4])
958 if int32(i)>>31 != 0 {
964 // boolAt returns the bool at the given offset in d.data.
965 func (d *decoder) boolAt(off int) bool {
966 return d.intAt(off) != 0
969 // stringTableAt returns the string pointed at by the int at the given offset in d.data.
970 func (d *decoder) stringAt(off int) string {
971 return d.stringTableAt(d.intAt(off))
974 // stringTableAt returns the string at the given offset in the string table d.str.
975 func (d *decoder) stringTableAt(off int) string {
976 if off < 0 || off >= len(d.str) {
980 v, n := binary.Uvarint(s)
981 if n <= 0 || v > uint64(len(s[n:])) {
984 return asString(s[n : n+int(v)])
987 // A reader reads sequential fields from a section of the index format.
993 // readAt returns a reader starting at the given position in d.
994 func (d *decoder) readAt(pos int) *reader {
995 return &reader{d, pos}
998 // int reads the next int.
999 func (r *reader) int() int {
1000 i := r.d.intAt(r.pos)
1005 // string reads the next string.
1006 func (r *reader) string() string {
1007 return r.d.stringTableAt(r.int())
1010 // bool reads the next bool.
1011 func (r *reader) bool() bool {
1015 // tokpos reads the next token.Position.
1016 func (r *reader) tokpos() token.Position {
1017 return token.Position{
1018 Filename: r.string(),