Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 1 | // Copyright 2018 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. |
| 4 | |
| 5 | // Indexed package import. |
| 6 | // See cmd/compile/internal/gc/iexport.go for the export data format. |
| 7 | |
| 8 | // This file is a copy of $GOROOT/src/go/internal/gcimporter/iimport.go. |
| 9 | |
| 10 | package gcimporter |
| 11 | |
| 12 | import ( |
| 13 | "bytes" |
| 14 | "encoding/binary" |
Robert Griesemer | 9a70f1f | 2018-06-12 22:33:41 -0700 | [diff] [blame] | 15 | "fmt" |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 16 | "go/constant" |
| 17 | "go/token" |
| 18 | "go/types" |
| 19 | "io" |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 20 | "math/big" |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 21 | "sort" |
Robert Findley | 3883e4a | 2021-09-30 22:45:40 -0400 | [diff] [blame] | 22 | "strings" |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 23 | |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 24 | "golang.org/x/tools/go/types/objectpath" |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 25 | "golang.org/x/tools/internal/typeparams" |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type intReader struct { |
| 29 | *bytes.Reader |
| 30 | path string |
| 31 | } |
| 32 | |
| 33 | func (r *intReader) int64() int64 { |
| 34 | i, err := binary.ReadVarint(r.Reader) |
| 35 | if err != nil { |
| 36 | errorf("import %q: read varint error: %v", r.path, err) |
| 37 | } |
| 38 | return i |
| 39 | } |
| 40 | |
| 41 | func (r *intReader) uint64() uint64 { |
| 42 | i, err := binary.ReadUvarint(r.Reader) |
| 43 | if err != nil { |
| 44 | errorf("import %q: read varint error: %v", r.path, err) |
| 45 | } |
| 46 | return i |
| 47 | } |
| 48 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 49 | // Keep this in sync with constants in iexport.go. |
| 50 | const ( |
Robert Findley | 52e9527 | 2022-01-10 18:46:59 -0500 | [diff] [blame] | 51 | iexportVersionGo1_11 = 0 |
| 52 | iexportVersionPosCol = 1 |
| 53 | iexportVersionGo1_18 = 2 |
| 54 | iexportVersionGenerics = 2 |
Alan Donovan | 207f456 | 2022-10-20 14:03:47 -0400 | [diff] [blame] | 55 | |
| 56 | iexportVersionCurrent = 2 |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 57 | ) |
| 58 | |
| 59 | type ident struct { |
Robert Findley | b4aba4b | 2022-04-14 15:47:16 -0400 | [diff] [blame] | 60 | pkg *types.Package |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 61 | name string |
| 62 | } |
| 63 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 64 | const predeclReserved = 32 |
| 65 | |
| 66 | type itag uint64 |
| 67 | |
| 68 | const ( |
| 69 | // Types |
| 70 | definedType itag = iota |
| 71 | pointerType |
| 72 | sliceType |
| 73 | arrayType |
| 74 | chanType |
| 75 | mapType |
| 76 | signatureType |
| 77 | structType |
| 78 | interfaceType |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 79 | typeParamType |
Robert Findley | 02e5238 | 2021-09-14 20:46:30 -0400 | [diff] [blame] | 80 | instanceType |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 81 | unionType |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 82 | ) |
| 83 | |
Robert Griesemer | 9a70f1f | 2018-06-12 22:33:41 -0700 | [diff] [blame] | 84 | // IImportData imports a package from the serialized package data |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 85 | // and returns 0 and a reference to the package. |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 86 | // If the export data version is not recognized or the format is otherwise |
| 87 | // compromised, an error is returned. |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 88 | func IImportData(fset *token.FileSet, imports map[string]*types.Package, data []byte, path string) (int, *types.Package, error) { |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 89 | pkgs, err := iimportCommon(fset, GetPackagesFromMap(imports), data, false, path, false, nil) |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 90 | if err != nil { |
| 91 | return 0, nil, err |
| 92 | } |
| 93 | return 0, pkgs[0], nil |
| 94 | } |
| 95 | |
| 96 | // IImportBundle imports a set of packages from the serialized package bundle. |
| 97 | func IImportBundle(fset *token.FileSet, imports map[string]*types.Package, data []byte) ([]*types.Package, error) { |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 98 | return iimportCommon(fset, GetPackagesFromMap(imports), data, true, "", false, nil) |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 101 | // A GetPackagesFunc function obtains the non-nil symbols for a set of |
| 102 | // packages, creating and recursively importing them as needed. An |
| 103 | // implementation should store each package symbol is in the Pkg |
| 104 | // field of the items array. |
Robert Findley | b6dbcf8 | 2023-03-15 10:27:49 -0400 | [diff] [blame] | 105 | // |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 106 | // Any error causes importing to fail. This can be used to quickly read |
| 107 | // the import manifest of an export data file without fully decoding it. |
| 108 | type GetPackagesFunc = func(items []GetPackagesItem) error |
Robert Findley | b6dbcf8 | 2023-03-15 10:27:49 -0400 | [diff] [blame] | 109 | |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 110 | // A GetPackagesItem is a request from the importer for the package |
| 111 | // symbol of the specified name and path. |
| 112 | type GetPackagesItem struct { |
| 113 | Name, Path string |
| 114 | Pkg *types.Package // to be filled in by GetPackagesFunc call |
| 115 | |
| 116 | // private importer state |
| 117 | pathOffset uint64 |
| 118 | nameIndex map[string]uint64 |
| 119 | } |
| 120 | |
| 121 | // GetPackagesFromMap returns a GetPackagesFunc that retrieves |
| 122 | // packages from the given map of package path to package. |
Robert Findley | b6dbcf8 | 2023-03-15 10:27:49 -0400 | [diff] [blame] | 123 | // |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 124 | // The returned function may mutate m: each requested package that is not |
| 125 | // found is created with types.NewPackage and inserted into m. |
| 126 | func GetPackagesFromMap(m map[string]*types.Package) GetPackagesFunc { |
| 127 | return func(items []GetPackagesItem) error { |
| 128 | for i, item := range items { |
| 129 | pkg, ok := m[item.Path] |
| 130 | if !ok { |
| 131 | pkg = types.NewPackage(item.Path, item.Name) |
| 132 | m[item.Path] = pkg |
| 133 | } |
| 134 | items[i].Pkg = pkg |
Robert Findley | b6dbcf8 | 2023-03-15 10:27:49 -0400 | [diff] [blame] | 135 | } |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 136 | return nil |
Robert Findley | b6dbcf8 | 2023-03-15 10:27:49 -0400 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 140 | func iimportCommon(fset *token.FileSet, getPackages GetPackagesFunc, data []byte, bundle bool, path string, shallow bool, reportf ReportFunc) (pkgs []*types.Package, err error) { |
Alan Donovan | 207f456 | 2022-10-20 14:03:47 -0400 | [diff] [blame] | 141 | const currentVersion = iexportVersionCurrent |
Matthew Dempsky | db0687c | 2019-09-26 17:38:16 -0700 | [diff] [blame] | 142 | version := int64(-1) |
Robert Findley | 91c880c | 2021-09-20 10:42:55 -0400 | [diff] [blame] | 143 | if !debug { |
| 144 | defer func() { |
| 145 | if e := recover(); e != nil { |
Russ Cox | e693fb4 | 2021-04-15 12:49:06 -0400 | [diff] [blame] | 146 | if bundle { |
| 147 | err = fmt.Errorf("%v", e) |
| 148 | } else if version > currentVersion { |
Robert Findley | 91c880c | 2021-09-20 10:42:55 -0400 | [diff] [blame] | 149 | err = fmt.Errorf("cannot import %q (%v), export data is newer version - update tool", path, e) |
| 150 | } else { |
Alan Donovan | 12a0517 | 2023-05-12 15:32:35 -0400 | [diff] [blame] | 151 | err = fmt.Errorf("internal error while importing %q (%v); please report an issue", path, e) |
Robert Findley | 91c880c | 2021-09-20 10:42:55 -0400 | [diff] [blame] | 152 | } |
Robert Griesemer | 9a70f1f | 2018-06-12 22:33:41 -0700 | [diff] [blame] | 153 | } |
Robert Findley | 91c880c | 2021-09-20 10:42:55 -0400 | [diff] [blame] | 154 | }() |
| 155 | } |
Robert Griesemer | 9a70f1f | 2018-06-12 22:33:41 -0700 | [diff] [blame] | 156 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 157 | r := &intReader{bytes.NewReader(data), path} |
| 158 | |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 159 | if bundle { |
Alan Donovan | 12a0517 | 2023-05-12 15:32:35 -0400 | [diff] [blame] | 160 | if v := r.uint64(); v != bundleVersion { |
| 161 | errorf("unknown bundle format version %d", v) |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Matthew Dempsky | db0687c | 2019-09-26 17:38:16 -0700 | [diff] [blame] | 165 | version = int64(r.uint64()) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 166 | switch version { |
Robert Findley | 18096c5 | 2021-10-22 11:02:26 -0400 | [diff] [blame] | 167 | case iexportVersionGo1_18, iexportVersionPosCol, iexportVersionGo1_11: |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 168 | default: |
Robert Findley | 18096c5 | 2021-10-22 11:02:26 -0400 | [diff] [blame] | 169 | if version > iexportVersionGo1_18 { |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 170 | errorf("unstable iexport format version %d, just rebuild compiler and std library", version) |
| 171 | } else { |
| 172 | errorf("unknown iexport format version %d", version) |
| 173 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | sLen := int64(r.uint64()) |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 177 | var fLen int64 |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 178 | var fileOffset []uint64 |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 179 | if shallow { |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 180 | // Shallow mode uses a different position encoding. |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 181 | fLen = int64(r.uint64()) |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 182 | fileOffset = make([]uint64, r.uint64()) |
| 183 | for i := range fileOffset { |
| 184 | fileOffset[i] = r.uint64() |
| 185 | } |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 186 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 187 | dLen := int64(r.uint64()) |
| 188 | |
| 189 | whence, _ := r.Seek(0, io.SeekCurrent) |
| 190 | stringData := data[whence : whence+sLen] |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 191 | fileData := data[whence+sLen : whence+sLen+fLen] |
| 192 | declData := data[whence+sLen+fLen : whence+sLen+fLen+dLen] |
| 193 | r.Seek(sLen+fLen+dLen, io.SeekCurrent) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 194 | |
| 195 | p := iimporter{ |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 196 | version: int(version), |
| 197 | ipath: path, |
| 198 | shallow: shallow, |
| 199 | reportf: reportf, |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 200 | |
| 201 | stringData: stringData, |
| 202 | stringCache: make(map[uint64]string), |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 203 | fileOffset: fileOffset, |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 204 | fileData: fileData, |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 205 | fileCache: make([]*token.File, len(fileOffset)), |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 206 | pkgCache: make(map[uint64]*types.Package), |
| 207 | |
| 208 | declData: declData, |
| 209 | pkgIndex: make(map[*types.Package]map[string]uint64), |
| 210 | typCache: make(map[uint64]types.Type), |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 211 | // Separate map for typeparams, keyed by their package and unique |
Robert Findley | 81f084e | 2021-10-18 12:23:03 -0400 | [diff] [blame] | 212 | // name. |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 213 | tparamIndex: make(map[ident]types.Type), |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 214 | |
| 215 | fake: fakeFileSet{ |
| 216 | fset: fset, |
Robert Findley | cbf1d01 | 2021-10-20 11:02:53 -0400 | [diff] [blame] | 217 | files: make(map[string]*fileInfo), |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 218 | }, |
| 219 | } |
Robert Findley | cbf1d01 | 2021-10-20 11:02:53 -0400 | [diff] [blame] | 220 | defer p.fake.setLines() // set lines for files in fset |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 221 | |
Rebecca Stambler | 58ecf64 | 2019-01-07 14:50:49 -0500 | [diff] [blame] | 222 | for i, pt := range predeclared() { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 223 | p.typCache[uint64(i)] = pt |
| 224 | } |
| 225 | |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 226 | // Gather the relevant packages from the manifest. |
| 227 | items := make([]GetPackagesItem, r.uint64()) |
| 228 | for i := range items { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 229 | pkgPathOff := r.uint64() |
| 230 | pkgPath := p.stringAt(pkgPathOff) |
| 231 | pkgName := p.stringAt(r.uint64()) |
| 232 | _ = r.uint64() // package height; unused by go/types |
| 233 | |
| 234 | if pkgPath == "" { |
| 235 | pkgPath = path |
| 236 | } |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 237 | items[i].Name = pkgName |
| 238 | items[i].Path = pkgPath |
| 239 | items[i].pathOffset = pkgPathOff |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 240 | |
Alan Donovan | 2b29c66 | 2022-11-03 14:55:29 -0400 | [diff] [blame] | 241 | // Read index for package. |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 242 | nameIndex := make(map[string]uint64) |
Alan Donovan | 2b29c66 | 2022-11-03 14:55:29 -0400 | [diff] [blame] | 243 | nSyms := r.uint64() |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 244 | // In shallow mode, only the current package (i=0) has an index. |
| 245 | assert(!(shallow && i > 0 && nSyms != 0)) |
Alan Donovan | 2b29c66 | 2022-11-03 14:55:29 -0400 | [diff] [blame] | 246 | for ; nSyms > 0; nSyms-- { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 247 | name := p.stringAt(r.uint64()) |
| 248 | nameIndex[name] = r.uint64() |
| 249 | } |
| 250 | |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 251 | items[i].nameIndex = nameIndex |
| 252 | } |
| 253 | |
| 254 | // Request packages all at once from the client, |
| 255 | // enabling a parallel implementation. |
| 256 | if err := getPackages(items); err != nil { |
| 257 | return nil, err // don't wrap this error |
| 258 | } |
| 259 | |
| 260 | // Check the results and complete the index. |
| 261 | pkgList := make([]*types.Package, len(items)) |
| 262 | for i, item := range items { |
| 263 | pkg := item.Pkg |
| 264 | if pkg == nil { |
| 265 | errorf("internal error: getPackages returned nil package for %q", item.Path) |
| 266 | } else if pkg.Path() != item.Path { |
| 267 | errorf("internal error: getPackages returned wrong path %q, want %q", pkg.Path(), item.Path) |
| 268 | } else if pkg.Name() != item.Name { |
| 269 | errorf("internal error: getPackages returned wrong name %s for package %q, want %s", pkg.Name(), item.Path, item.Name) |
| 270 | } |
| 271 | p.pkgCache[item.pathOffset] = pkg |
| 272 | p.pkgIndex[pkg] = item.nameIndex |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 273 | pkgList[i] = pkg |
| 274 | } |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 275 | |
| 276 | if bundle { |
| 277 | pkgs = make([]*types.Package, r.uint64()) |
| 278 | for i := range pkgs { |
| 279 | pkg := p.pkgAt(r.uint64()) |
| 280 | imps := make([]*types.Package, r.uint64()) |
| 281 | for j := range imps { |
| 282 | imps[j] = p.pkgAt(r.uint64()) |
| 283 | } |
| 284 | pkg.SetImports(imps) |
| 285 | pkgs[i] = pkg |
| 286 | } |
| 287 | } else { |
| 288 | if len(pkgList) == 0 { |
| 289 | errorf("no packages found for %s", path) |
| 290 | panic("unreachable") |
| 291 | } |
| 292 | pkgs = pkgList[:1] |
| 293 | |
| 294 | // record all referenced packages as imports |
| 295 | list := append(([]*types.Package)(nil), pkgList[1:]...) |
| 296 | sort.Sort(byPath(list)) |
| 297 | pkgs[0].SetImports(list) |
Rebecca Stambler | 246a69f | 2019-10-03 17:28:33 -0400 | [diff] [blame] | 298 | } |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 299 | |
| 300 | for _, pkg := range pkgs { |
| 301 | if pkg.Complete() { |
| 302 | continue |
| 303 | } |
| 304 | |
| 305 | names := make([]string, 0, len(p.pkgIndex[pkg])) |
| 306 | for name := range p.pkgIndex[pkg] { |
| 307 | names = append(names, name) |
| 308 | } |
| 309 | sort.Strings(names) |
| 310 | for _, name := range names { |
| 311 | p.doDecl(pkg, name) |
| 312 | } |
| 313 | |
| 314 | // package was imported completely and without errors |
| 315 | pkg.MarkComplete() |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 316 | } |
| 317 | |
Robert Findley | dff7c5f | 2022-02-22 09:47:31 -0500 | [diff] [blame] | 318 | // SetConstraint can't be called if the constraint type is not yet complete. |
| 319 | // When type params are created in the 'P' case of (*importReader).obj(), |
| 320 | // the associated constraint type may not be complete due to recursion. |
| 321 | // Therefore, we defer calling SetConstraint there, and call it here instead |
| 322 | // after all types are complete. |
| 323 | for _, d := range p.later { |
| 324 | typeparams.SetTypeParamConstraint(d.t, d.constraint) |
| 325 | } |
| 326 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 327 | for _, typ := range p.interfaceList { |
| 328 | typ.Complete() |
| 329 | } |
| 330 | |
Rob Findley | 07bfcd4 | 2023-07-25 10:44:06 -0400 | [diff] [blame] | 331 | // Workaround for golang/go#61561. See the doc for instanceList for details. |
| 332 | for _, typ := range p.instanceList { |
| 333 | if iface, _ := typ.Underlying().(*types.Interface); iface != nil { |
| 334 | iface.Complete() |
| 335 | } |
| 336 | } |
| 337 | |
Matthew Dempsky | a1db63c | 2021-02-15 22:17:43 -0800 | [diff] [blame] | 338 | return pkgs, nil |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Robert Findley | dff7c5f | 2022-02-22 09:47:31 -0500 | [diff] [blame] | 341 | type setConstraintArgs struct { |
| 342 | t *typeparams.TypeParam |
| 343 | constraint types.Type |
| 344 | } |
| 345 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 346 | type iimporter struct { |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 347 | version int |
| 348 | ipath string |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 349 | |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 350 | shallow bool |
| 351 | reportf ReportFunc // if non-nil, used to report bugs |
Alan Donovan | 2b29c66 | 2022-11-03 14:55:29 -0400 | [diff] [blame] | 352 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 353 | stringData []byte |
| 354 | stringCache map[uint64]string |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 355 | fileOffset []uint64 // fileOffset[i] is offset in fileData for info about file encoded as i |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 356 | fileData []byte |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 357 | fileCache []*token.File // memoized decoding of file encoded as i |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 358 | pkgCache map[uint64]*types.Package |
| 359 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 360 | declData []byte |
| 361 | pkgIndex map[*types.Package]map[string]uint64 |
| 362 | typCache map[uint64]types.Type |
| 363 | tparamIndex map[ident]types.Type |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 364 | |
| 365 | fake fakeFileSet |
| 366 | interfaceList []*types.Interface |
Robert Findley | 81f084e | 2021-10-18 12:23:03 -0400 | [diff] [blame] | 367 | |
Rob Findley | 07bfcd4 | 2023-07-25 10:44:06 -0400 | [diff] [blame] | 368 | // Workaround for the go/types bug golang/go#61561: instances produced during |
| 369 | // instantiation may contain incomplete interfaces. Here we only complete the |
| 370 | // underlying type of the instance, which is the most common case but doesn't |
| 371 | // handle parameterized interface literals defined deeper in the type. |
| 372 | instanceList []types.Type // instances for later completion (see golang/go#61561) |
| 373 | |
Robert Findley | dff7c5f | 2022-02-22 09:47:31 -0500 | [diff] [blame] | 374 | // Arguments for calls to SetConstraint that are deferred due to recursive types |
| 375 | later []setConstraintArgs |
| 376 | |
Robert Findley | 81f084e | 2021-10-18 12:23:03 -0400 | [diff] [blame] | 377 | indent int // for tracing support |
| 378 | } |
| 379 | |
| 380 | func (p *iimporter) trace(format string, args ...interface{}) { |
| 381 | if !trace { |
| 382 | // Call sites should also be guarded, but having this check here allows |
| 383 | // easily enabling/disabling debug trace statements. |
| 384 | return |
| 385 | } |
| 386 | fmt.Printf(strings.Repeat("..", p.indent)+format+"\n", args...) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | func (p *iimporter) doDecl(pkg *types.Package, name string) { |
Robert Findley | 81f084e | 2021-10-18 12:23:03 -0400 | [diff] [blame] | 390 | if debug { |
| 391 | p.trace("import decl %s", name) |
| 392 | p.indent++ |
| 393 | defer func() { |
| 394 | p.indent-- |
| 395 | p.trace("=> %s", name) |
| 396 | }() |
| 397 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 398 | // See if we've already imported this declaration. |
| 399 | if obj := pkg.Scope().Lookup(name); obj != nil { |
| 400 | return |
| 401 | } |
| 402 | |
| 403 | off, ok := p.pkgIndex[pkg][name] |
| 404 | if !ok { |
Alan Donovan | b71392a | 2023-06-13 11:47:20 -0400 | [diff] [blame] | 405 | // In deep mode, the index should be complete. In shallow |
| 406 | // mode, we should have already recursively loaded necessary |
| 407 | // dependencies so the above Lookup succeeds. |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 408 | errorf("%v.%v not in index", pkg, name) |
| 409 | } |
| 410 | |
| 411 | r := &importReader{p: p, currPkg: pkg} |
| 412 | r.declReader.Reset(p.declData[off:]) |
| 413 | |
| 414 | r.obj(name) |
| 415 | } |
| 416 | |
| 417 | func (p *iimporter) stringAt(off uint64) string { |
| 418 | if s, ok := p.stringCache[off]; ok { |
| 419 | return s |
| 420 | } |
| 421 | |
| 422 | slen, n := binary.Uvarint(p.stringData[off:]) |
| 423 | if n <= 0 { |
| 424 | errorf("varint failed") |
| 425 | } |
| 426 | spos := off + uint64(n) |
| 427 | s := string(p.stringData[spos : spos+slen]) |
| 428 | p.stringCache[off] = s |
| 429 | return s |
| 430 | } |
| 431 | |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 432 | func (p *iimporter) fileAt(index uint64) *token.File { |
| 433 | file := p.fileCache[index] |
| 434 | if file == nil { |
| 435 | off := p.fileOffset[index] |
Alan Donovan | f0e2d5c | 2023-01-13 15:12:19 -0500 | [diff] [blame] | 436 | file = p.decodeFile(intReader{bytes.NewReader(p.fileData[off:]), p.ipath}) |
Alan Donovan | d958e85 | 2023-01-13 11:44:12 -0500 | [diff] [blame] | 437 | p.fileCache[index] = file |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 438 | } |
| 439 | return file |
| 440 | } |
| 441 | |
Alan Donovan | f0e2d5c | 2023-01-13 15:12:19 -0500 | [diff] [blame] | 442 | func (p *iimporter) decodeFile(rd intReader) *token.File { |
| 443 | filename := p.stringAt(rd.uint64()) |
| 444 | size := int(rd.uint64()) |
| 445 | file := p.fake.fset.AddFile(filename, -1, size) |
| 446 | |
| 447 | // SetLines requires a nondecreasing sequence. |
| 448 | // Because it is common for clients to derive the interval |
| 449 | // [start, start+len(name)] from a start position, and we |
| 450 | // want to ensure that the end offset is on the same line, |
| 451 | // we fill in the gaps of the sparse encoding with values |
| 452 | // that strictly increase by the largest possible amount. |
| 453 | // This allows us to avoid having to record the actual end |
| 454 | // offset of each needed line. |
| 455 | |
| 456 | lines := make([]int, int(rd.uint64())) |
| 457 | var index, offset int |
| 458 | for i, n := 0, int(rd.uint64()); i < n; i++ { |
| 459 | index += int(rd.uint64()) |
| 460 | offset += int(rd.uint64()) |
| 461 | lines[index] = offset |
| 462 | |
| 463 | // Ensure monotonicity between points. |
| 464 | for j := index - 1; j > 0 && lines[j] == 0; j-- { |
| 465 | lines[j] = lines[j+1] - 1 |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // Ensure monotonicity after last point. |
| 470 | for j := len(lines) - 1; j > 0 && lines[j] == 0; j-- { |
| 471 | size-- |
| 472 | lines[j] = size |
| 473 | } |
| 474 | |
| 475 | if !file.SetLines(lines) { |
| 476 | errorf("SetLines failed: %d", lines) // can't happen |
| 477 | } |
| 478 | return file |
| 479 | } |
| 480 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 481 | func (p *iimporter) pkgAt(off uint64) *types.Package { |
| 482 | if pkg, ok := p.pkgCache[off]; ok { |
| 483 | return pkg |
| 484 | } |
| 485 | path := p.stringAt(off) |
| 486 | errorf("missing package %q in %q", path, p.ipath) |
| 487 | return nil |
| 488 | } |
| 489 | |
| 490 | func (p *iimporter) typAt(off uint64, base *types.Named) types.Type { |
Robert Findley | df48029 | 2021-12-01 13:55:45 -0500 | [diff] [blame] | 491 | if t, ok := p.typCache[off]; ok && canReuse(base, t) { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 492 | return t |
| 493 | } |
| 494 | |
| 495 | if off < predeclReserved { |
| 496 | errorf("predeclared type missing from cache: %v", off) |
| 497 | } |
| 498 | |
| 499 | r := &importReader{p: p} |
| 500 | r.declReader.Reset(p.declData[off-predeclReserved:]) |
| 501 | t := r.doType(base) |
| 502 | |
Robert Findley | df48029 | 2021-12-01 13:55:45 -0500 | [diff] [blame] | 503 | if canReuse(base, t) { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 504 | p.typCache[off] = t |
| 505 | } |
| 506 | return t |
| 507 | } |
| 508 | |
Robert Findley | df48029 | 2021-12-01 13:55:45 -0500 | [diff] [blame] | 509 | // canReuse reports whether the type rhs on the RHS of the declaration for def |
| 510 | // may be re-used. |
| 511 | // |
| 512 | // Specifically, if def is non-nil and rhs is an interface type with methods, it |
| 513 | // may not be re-used because we have a convention of setting the receiver type |
| 514 | // for interface methods to def. |
| 515 | func canReuse(def *types.Named, rhs types.Type) bool { |
| 516 | if def == nil { |
| 517 | return true |
| 518 | } |
| 519 | iface, _ := rhs.(*types.Interface) |
| 520 | if iface == nil { |
| 521 | return true |
| 522 | } |
| 523 | // Don't use iface.Empty() here as iface may not be complete. |
| 524 | return iface.NumEmbeddeds() == 0 && iface.NumExplicitMethods() == 0 |
| 525 | } |
| 526 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 527 | type importReader struct { |
| 528 | p *iimporter |
| 529 | declReader bytes.Reader |
| 530 | currPkg *types.Package |
| 531 | prevFile string |
| 532 | prevLine int64 |
Matthew Dempsky | db0687c | 2019-09-26 17:38:16 -0700 | [diff] [blame] | 533 | prevColumn int64 |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | func (r *importReader) obj(name string) { |
| 537 | tag := r.byte() |
| 538 | pos := r.pos() |
| 539 | |
| 540 | switch tag { |
| 541 | case 'A': |
| 542 | typ := r.typ() |
| 543 | |
| 544 | r.declare(types.NewTypeName(pos, r.currPkg, name, typ)) |
| 545 | |
| 546 | case 'C': |
| 547 | typ, val := r.value() |
| 548 | |
| 549 | r.declare(types.NewConst(pos, r.currPkg, name, typ, val)) |
| 550 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 551 | case 'F', 'G': |
| 552 | var tparams []*typeparams.TypeParam |
| 553 | if tag == 'G' { |
| 554 | tparams = r.tparamList() |
| 555 | } |
Robert Findley | 2189684 | 2021-09-28 15:04:48 -0400 | [diff] [blame] | 556 | sig := r.signature(nil, nil, tparams) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 557 | r.declare(types.NewFunc(pos, r.currPkg, name, sig)) |
| 558 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 559 | case 'T', 'U': |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 560 | // Types can be recursive. We need to setup a stub |
| 561 | // declaration before recursing. |
| 562 | obj := types.NewTypeName(pos, r.currPkg, name, nil) |
| 563 | named := types.NewNamed(obj, nil, nil) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 564 | // Declare obj before calling r.tparamList, so the new type name is recognized |
| 565 | // if used in the constraint of one of its own typeparams (see #48280). |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 566 | r.declare(obj) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 567 | if tag == 'U' { |
| 568 | tparams := r.tparamList() |
| 569 | typeparams.SetForNamed(named, tparams) |
| 570 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 571 | |
| 572 | underlying := r.p.typAt(r.uint64(), named).Underlying() |
| 573 | named.SetUnderlying(underlying) |
| 574 | |
| 575 | if !isInterface(underlying) { |
| 576 | for n := r.uint64(); n > 0; n-- { |
| 577 | mpos := r.pos() |
| 578 | mname := r.ident() |
| 579 | recv := r.param() |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 580 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 581 | // If the receiver has any targs, set those as the |
| 582 | // rparams of the method (since those are the |
| 583 | // typeparams being used in the method sig/body). |
Robert Findley | 81f084e | 2021-10-18 12:23:03 -0400 | [diff] [blame] | 584 | base := baseType(recv.Type()) |
| 585 | assert(base != nil) |
| 586 | targs := typeparams.NamedTypeArgs(base) |
Robert Findley | 2189684 | 2021-09-28 15:04:48 -0400 | [diff] [blame] | 587 | var rparams []*typeparams.TypeParam |
Robert Findley | 91c880c | 2021-09-20 10:42:55 -0400 | [diff] [blame] | 588 | if targs.Len() > 0 { |
Robert Findley | 81f084e | 2021-10-18 12:23:03 -0400 | [diff] [blame] | 589 | rparams = make([]*typeparams.TypeParam, targs.Len()) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 590 | for i := range rparams { |
Robert Findley | 91c880c | 2021-09-20 10:42:55 -0400 | [diff] [blame] | 591 | rparams[i] = targs.At(i).(*typeparams.TypeParam) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 592 | } |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 593 | } |
Robert Findley | 2189684 | 2021-09-28 15:04:48 -0400 | [diff] [blame] | 594 | msig := r.signature(recv, rparams, nil) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 595 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 596 | named.AddMethod(types.NewFunc(mpos, r.currPkg, mname, msig)) |
| 597 | } |
| 598 | } |
| 599 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 600 | case 'P': |
| 601 | // We need to "declare" a typeparam in order to have a name that |
| 602 | // can be referenced recursively (if needed) in the type param's |
| 603 | // bound. |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 604 | if r.p.version < iexportVersionGenerics { |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 605 | errorf("unexpected type param type") |
| 606 | } |
Robert Findley | 80963bc | 2022-01-20 11:16:42 -0500 | [diff] [blame] | 607 | name0 := tparamName(name) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 608 | tn := types.NewTypeName(pos, r.currPkg, name0, nil) |
| 609 | t := typeparams.NewTypeParam(tn, nil) |
Robert Findley | 3883e4a | 2021-09-30 22:45:40 -0400 | [diff] [blame] | 610 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 611 | // To handle recursive references to the typeparam within its |
| 612 | // bound, save the partial type in tparamIndex before reading the bounds. |
Robert Findley | b4aba4b | 2022-04-14 15:47:16 -0400 | [diff] [blame] | 613 | id := ident{r.currPkg, name} |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 614 | r.p.tparamIndex[id] = t |
Robert Findley | 18096c5 | 2021-10-22 11:02:26 -0400 | [diff] [blame] | 615 | var implicit bool |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 616 | if r.p.version >= iexportVersionGo1_18 { |
Robert Findley | 18096c5 | 2021-10-22 11:02:26 -0400 | [diff] [blame] | 617 | implicit = r.bool() |
| 618 | } |
| 619 | constraint := r.typ() |
| 620 | if implicit { |
| 621 | iface, _ := constraint.(*types.Interface) |
| 622 | if iface == nil { |
| 623 | errorf("non-interface constraint marked implicit") |
| 624 | } |
| 625 | typeparams.MarkImplicit(iface) |
| 626 | } |
Robert Findley | dff7c5f | 2022-02-22 09:47:31 -0500 | [diff] [blame] | 627 | // The constraint type may not be complete, if we |
| 628 | // are in the middle of a type recursion involving type |
| 629 | // constraints. So, we defer SetConstraint until we have |
| 630 | // completely set up all types in ImportData. |
| 631 | r.p.later = append(r.p.later, setConstraintArgs{t: t, constraint: constraint}) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 632 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 633 | case 'V': |
| 634 | typ := r.typ() |
| 635 | |
| 636 | r.declare(types.NewVar(pos, r.currPkg, name, typ)) |
| 637 | |
| 638 | default: |
| 639 | errorf("unexpected tag: %v", tag) |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | func (r *importReader) declare(obj types.Object) { |
| 644 | obj.Pkg().Scope().Insert(obj) |
| 645 | } |
| 646 | |
| 647 | func (r *importReader) value() (typ types.Type, val constant.Value) { |
| 648 | typ = r.typ() |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 649 | if r.p.version >= iexportVersionGo1_18 { |
| 650 | // TODO: add support for using the kind. |
| 651 | _ = constant.Kind(r.int64()) |
| 652 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 653 | |
| 654 | switch b := typ.Underlying().(*types.Basic); b.Info() & types.IsConstType { |
| 655 | case types.IsBoolean: |
| 656 | val = constant.MakeBool(r.bool()) |
| 657 | |
| 658 | case types.IsString: |
| 659 | val = constant.MakeString(r.string()) |
| 660 | |
| 661 | case types.IsInteger: |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 662 | var x big.Int |
| 663 | r.mpint(&x, b) |
| 664 | val = constant.Make(&x) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 665 | |
| 666 | case types.IsFloat: |
| 667 | val = r.mpfloat(b) |
| 668 | |
| 669 | case types.IsComplex: |
| 670 | re := r.mpfloat(b) |
| 671 | im := r.mpfloat(b) |
| 672 | val = constant.BinaryOp(re, token.ADD, constant.MakeImag(im)) |
| 673 | |
| 674 | default: |
Rebecca Stambler | 58ecf64 | 2019-01-07 14:50:49 -0500 | [diff] [blame] | 675 | if b.Kind() == types.Invalid { |
| 676 | val = constant.MakeUnknown() |
| 677 | return |
| 678 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 679 | errorf("unexpected type %v", typ) // panics |
| 680 | panic("unreachable") |
| 681 | } |
| 682 | |
| 683 | return |
| 684 | } |
| 685 | |
| 686 | func intSize(b *types.Basic) (signed bool, maxBytes uint) { |
| 687 | if (b.Info() & types.IsUntyped) != 0 { |
| 688 | return true, 64 |
| 689 | } |
| 690 | |
| 691 | switch b.Kind() { |
| 692 | case types.Float32, types.Complex64: |
| 693 | return true, 3 |
| 694 | case types.Float64, types.Complex128: |
| 695 | return true, 7 |
| 696 | } |
| 697 | |
| 698 | signed = (b.Info() & types.IsUnsigned) == 0 |
| 699 | switch b.Kind() { |
| 700 | case types.Int8, types.Uint8: |
| 701 | maxBytes = 1 |
| 702 | case types.Int16, types.Uint16: |
| 703 | maxBytes = 2 |
| 704 | case types.Int32, types.Uint32: |
| 705 | maxBytes = 4 |
| 706 | default: |
| 707 | maxBytes = 8 |
| 708 | } |
| 709 | |
| 710 | return |
| 711 | } |
| 712 | |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 713 | func (r *importReader) mpint(x *big.Int, typ *types.Basic) { |
| 714 | signed, maxBytes := intSize(typ) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 715 | |
| 716 | maxSmall := 256 - maxBytes |
| 717 | if signed { |
| 718 | maxSmall = 256 - 2*maxBytes |
| 719 | } |
| 720 | if maxBytes == 1 { |
| 721 | maxSmall = 256 |
| 722 | } |
| 723 | |
| 724 | n, _ := r.declReader.ReadByte() |
| 725 | if uint(n) < maxSmall { |
| 726 | v := int64(n) |
| 727 | if signed { |
| 728 | v >>= 1 |
| 729 | if n&1 != 0 { |
| 730 | v = ^v |
| 731 | } |
| 732 | } |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 733 | x.SetInt64(v) |
| 734 | return |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | v := -n |
| 738 | if signed { |
| 739 | v = -(n &^ 1) >> 1 |
| 740 | } |
| 741 | if v < 1 || uint(v) > maxBytes { |
| 742 | errorf("weird decoding: %v, %v => %v", n, signed, v) |
| 743 | } |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 744 | b := make([]byte, v) |
| 745 | io.ReadFull(&r.declReader, b) |
| 746 | x.SetBytes(b) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 747 | if signed && n&1 != 0 { |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 748 | x.Neg(x) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 749 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 750 | } |
| 751 | |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 752 | func (r *importReader) mpfloat(typ *types.Basic) constant.Value { |
| 753 | var mant big.Int |
| 754 | r.mpint(&mant, typ) |
| 755 | var f big.Float |
| 756 | f.SetInt(&mant) |
| 757 | if f.Sign() != 0 { |
| 758 | f.SetMantExp(&f, int(r.int64())) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 759 | } |
Robert Findley | 32129bf | 2022-07-14 19:30:14 -0400 | [diff] [blame] | 760 | return constant.Make(&f) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | func (r *importReader) ident() string { |
| 764 | return r.string() |
| 765 | } |
| 766 | |
| 767 | func (r *importReader) qualifiedIdent() (*types.Package, string) { |
| 768 | name := r.string() |
| 769 | pkg := r.pkg() |
| 770 | return pkg, name |
| 771 | } |
| 772 | |
| 773 | func (r *importReader) pos() token.Pos { |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 774 | if r.p.shallow { |
| 775 | // precise offsets are encoded only in shallow mode |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 776 | return r.posv2() |
| 777 | } |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 778 | if r.p.version >= iexportVersionPosCol { |
Matthew Dempsky | db0687c | 2019-09-26 17:38:16 -0700 | [diff] [blame] | 779 | r.posv1() |
| 780 | } else { |
| 781 | r.posv0() |
| 782 | } |
| 783 | |
| 784 | if r.prevFile == "" && r.prevLine == 0 && r.prevColumn == 0 { |
| 785 | return token.NoPos |
| 786 | } |
| 787 | return r.p.fake.pos(r.prevFile, int(r.prevLine), int(r.prevColumn)) |
| 788 | } |
| 789 | |
| 790 | func (r *importReader) posv0() { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 791 | delta := r.int64() |
| 792 | if delta != deltaNewFile { |
| 793 | r.prevLine += delta |
| 794 | } else if l := r.int64(); l == -1 { |
| 795 | r.prevLine += deltaNewFile |
| 796 | } else { |
| 797 | r.prevFile = r.string() |
| 798 | r.prevLine = l |
| 799 | } |
Matthew Dempsky | db0687c | 2019-09-26 17:38:16 -0700 | [diff] [blame] | 800 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 801 | |
Matthew Dempsky | db0687c | 2019-09-26 17:38:16 -0700 | [diff] [blame] | 802 | func (r *importReader) posv1() { |
| 803 | delta := r.int64() |
| 804 | r.prevColumn += delta >> 1 |
| 805 | if delta&1 != 0 { |
| 806 | delta = r.int64() |
| 807 | r.prevLine += delta >> 1 |
| 808 | if delta&1 != 0 { |
| 809 | r.prevFile = r.string() |
| 810 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 811 | } |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 812 | } |
| 813 | |
Alan Donovan | 8aba49b | 2023-01-09 13:44:29 -0500 | [diff] [blame] | 814 | func (r *importReader) posv2() token.Pos { |
| 815 | file := r.uint64() |
| 816 | if file == 0 { |
| 817 | return token.NoPos |
| 818 | } |
| 819 | tf := r.p.fileAt(file - 1) |
| 820 | return tf.Pos(int(r.uint64())) |
| 821 | } |
| 822 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 823 | func (r *importReader) typ() types.Type { |
| 824 | return r.p.typAt(r.uint64(), nil) |
| 825 | } |
| 826 | |
| 827 | func isInterface(t types.Type) bool { |
| 828 | _, ok := t.(*types.Interface) |
| 829 | return ok |
| 830 | } |
| 831 | |
| 832 | func (r *importReader) pkg() *types.Package { return r.p.pkgAt(r.uint64()) } |
| 833 | func (r *importReader) string() string { return r.p.stringAt(r.uint64()) } |
| 834 | |
Robert Findley | 81f084e | 2021-10-18 12:23:03 -0400 | [diff] [blame] | 835 | func (r *importReader) doType(base *types.Named) (res types.Type) { |
| 836 | k := r.kind() |
| 837 | if debug { |
| 838 | r.p.trace("importing type %d (base: %s)", k, base) |
| 839 | r.p.indent++ |
| 840 | defer func() { |
| 841 | r.p.indent-- |
| 842 | r.p.trace("=> %s", res) |
| 843 | }() |
| 844 | } |
| 845 | switch k { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 846 | default: |
| 847 | errorf("unexpected kind tag in %q: %v", r.p.ipath, k) |
| 848 | return nil |
| 849 | |
| 850 | case definedType: |
| 851 | pkg, name := r.qualifiedIdent() |
| 852 | r.p.doDecl(pkg, name) |
| 853 | return pkg.Scope().Lookup(name).(*types.TypeName).Type() |
| 854 | case pointerType: |
| 855 | return types.NewPointer(r.typ()) |
| 856 | case sliceType: |
| 857 | return types.NewSlice(r.typ()) |
| 858 | case arrayType: |
| 859 | n := r.uint64() |
| 860 | return types.NewArray(r.typ(), int64(n)) |
| 861 | case chanType: |
| 862 | dir := chanDir(int(r.uint64())) |
| 863 | return types.NewChan(dir, r.typ()) |
| 864 | case mapType: |
| 865 | return types.NewMap(r.typ(), r.typ()) |
| 866 | case signatureType: |
| 867 | r.currPkg = r.pkg() |
Robert Findley | 2189684 | 2021-09-28 15:04:48 -0400 | [diff] [blame] | 868 | return r.signature(nil, nil, nil) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 869 | |
| 870 | case structType: |
| 871 | r.currPkg = r.pkg() |
| 872 | |
| 873 | fields := make([]*types.Var, r.uint64()) |
| 874 | tags := make([]string, len(fields)) |
| 875 | for i := range fields { |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 876 | var field *types.Var |
| 877 | if r.p.shallow { |
| 878 | field, _ = r.objectPathObject().(*types.Var) |
| 879 | } |
| 880 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 881 | fpos := r.pos() |
| 882 | fname := r.ident() |
| 883 | ftyp := r.typ() |
| 884 | emb := r.bool() |
| 885 | tag := r.string() |
| 886 | |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 887 | // Either this is not a shallow import, the field is local, or the |
| 888 | // encoded objectPath failed to produce an object (a bug). |
| 889 | // |
| 890 | // Even in this last, buggy case, fall back on creating a new field. As |
| 891 | // discussed in iexport.go, this is not correct, but mostly works and is |
| 892 | // preferable to failing (for now at least). |
| 893 | if field == nil { |
| 894 | field = types.NewField(fpos, r.currPkg, fname, ftyp, emb) |
| 895 | } |
| 896 | |
| 897 | fields[i] = field |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 898 | tags[i] = tag |
| 899 | } |
| 900 | return types.NewStruct(fields, tags) |
| 901 | |
| 902 | case interfaceType: |
| 903 | r.currPkg = r.pkg() |
| 904 | |
Robert Griesemer | 9a70f1f | 2018-06-12 22:33:41 -0700 | [diff] [blame] | 905 | embeddeds := make([]types.Type, r.uint64()) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 906 | for i := range embeddeds { |
| 907 | _ = r.pos() |
Robert Griesemer | 9a70f1f | 2018-06-12 22:33:41 -0700 | [diff] [blame] | 908 | embeddeds[i] = r.typ() |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | methods := make([]*types.Func, r.uint64()) |
| 912 | for i := range methods { |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 913 | var method *types.Func |
| 914 | if r.p.shallow { |
| 915 | method, _ = r.objectPathObject().(*types.Func) |
| 916 | } |
| 917 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 918 | mpos := r.pos() |
| 919 | mname := r.ident() |
| 920 | |
| 921 | // TODO(mdempsky): Matches bimport.go, but I |
| 922 | // don't agree with this. |
| 923 | var recv *types.Var |
| 924 | if base != nil { |
| 925 | recv = types.NewVar(token.NoPos, r.currPkg, "", base) |
| 926 | } |
Robert Findley | 2189684 | 2021-09-28 15:04:48 -0400 | [diff] [blame] | 927 | msig := r.signature(recv, nil, nil) |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 928 | |
| 929 | if method == nil { |
| 930 | method = types.NewFunc(mpos, r.currPkg, mname, msig) |
| 931 | } |
| 932 | methods[i] = method |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 933 | } |
| 934 | |
Robert Griesemer | 9d85510 | 2018-06-13 10:56:57 -0700 | [diff] [blame] | 935 | typ := newInterface(methods, embeddeds) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 936 | r.p.interfaceList = append(r.p.interfaceList, typ) |
| 937 | return typ |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 938 | |
| 939 | case typeParamType: |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 940 | if r.p.version < iexportVersionGenerics { |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 941 | errorf("unexpected type param type") |
| 942 | } |
| 943 | pkg, name := r.qualifiedIdent() |
Robert Findley | b4aba4b | 2022-04-14 15:47:16 -0400 | [diff] [blame] | 944 | id := ident{pkg, name} |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 945 | if t, ok := r.p.tparamIndex[id]; ok { |
| 946 | // We're already in the process of importing this typeparam. |
| 947 | return t |
| 948 | } |
| 949 | // Otherwise, import the definition of the typeparam now. |
| 950 | r.p.doDecl(pkg, name) |
| 951 | return r.p.tparamIndex[id] |
| 952 | |
Robert Findley | 02e5238 | 2021-09-14 20:46:30 -0400 | [diff] [blame] | 953 | case instanceType: |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 954 | if r.p.version < iexportVersionGenerics { |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 955 | errorf("unexpected instantiation type") |
| 956 | } |
| 957 | // pos does not matter for instances: they are positioned on the original |
| 958 | // type. |
| 959 | _ = r.pos() |
| 960 | len := r.uint64() |
| 961 | targs := make([]types.Type, len) |
| 962 | for i := range targs { |
| 963 | targs[i] = r.typ() |
| 964 | } |
| 965 | baseType := r.typ() |
| 966 | // The imported instantiated type doesn't include any methods, so |
| 967 | // we must always use the methods of the base (orig) type. |
| 968 | // TODO provide a non-nil *Environment |
| 969 | t, _ := typeparams.Instantiate(nil, baseType, targs, false) |
Rob Findley | 07bfcd4 | 2023-07-25 10:44:06 -0400 | [diff] [blame] | 970 | |
| 971 | // Workaround for golang/go#61561. See the doc for instanceList for details. |
| 972 | r.p.instanceList = append(r.p.instanceList, t) |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 973 | return t |
| 974 | |
| 975 | case unionType: |
Robert Findley | fc8b4ca | 2021-10-22 11:32:16 -0400 | [diff] [blame] | 976 | if r.p.version < iexportVersionGenerics { |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 977 | errorf("unexpected instantiation type") |
| 978 | } |
| 979 | terms := make([]*typeparams.Term, r.uint64()) |
| 980 | for i := range terms { |
| 981 | terms[i] = typeparams.NewTerm(r.bool(), r.typ()) |
| 982 | } |
| 983 | return typeparams.NewUnion(terms) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | |
| 987 | func (r *importReader) kind() itag { |
| 988 | return itag(r.uint64()) |
| 989 | } |
| 990 | |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 991 | // objectPathObject is the inverse of exportWriter.objectPath. |
| 992 | // |
| 993 | // In shallow mode, certain fields and methods may need to be looked up in an |
| 994 | // imported package. See the doc for exportWriter.objectPath for a full |
| 995 | // explanation. |
| 996 | func (r *importReader) objectPathObject() types.Object { |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 997 | objPath := objectpath.Path(r.string()) |
| 998 | if objPath == "" { |
| 999 | return nil |
| 1000 | } |
Rob Findley | 48026e1 | 2023-07-17 18:01:44 -0400 | [diff] [blame] | 1001 | pkg := r.pkg() |
Rob Findley | 89d9fae | 2023-06-15 17:10:58 -0400 | [diff] [blame] | 1002 | obj, err := objectpath.Object(pkg, objPath) |
| 1003 | if err != nil { |
| 1004 | if r.p.reportf != nil { |
| 1005 | r.p.reportf("failed to find object for objectPath %q: %v", objPath, err) |
| 1006 | } |
| 1007 | } |
| 1008 | return obj |
| 1009 | } |
| 1010 | |
Robert Findley | 2189684 | 2021-09-28 15:04:48 -0400 | [diff] [blame] | 1011 | func (r *importReader) signature(recv *types.Var, rparams []*typeparams.TypeParam, tparams []*typeparams.TypeParam) *types.Signature { |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 1012 | params := r.paramList() |
| 1013 | results := r.paramList() |
| 1014 | variadic := params.Len() > 0 && r.bool() |
Robert Findley | 2189684 | 2021-09-28 15:04:48 -0400 | [diff] [blame] | 1015 | return typeparams.NewSignatureType(recv, rparams, tparams, params, results, variadic) |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 1016 | } |
| 1017 | |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 1018 | func (r *importReader) tparamList() []*typeparams.TypeParam { |
| 1019 | n := r.uint64() |
| 1020 | if n == 0 { |
| 1021 | return nil |
| 1022 | } |
| 1023 | xs := make([]*typeparams.TypeParam, n) |
| 1024 | for i := range xs { |
| 1025 | // Note: the standard library importer is tolerant of nil types here, |
| 1026 | // though would panic in SetTypeParams. |
| 1027 | xs[i] = r.typ().(*typeparams.TypeParam) |
| 1028 | } |
| 1029 | return xs |
| 1030 | } |
| 1031 | |
Rebecca Stambler | 6e0e218 | 2018-04-26 13:58:52 -0400 | [diff] [blame] | 1032 | func (r *importReader) paramList() *types.Tuple { |
| 1033 | xs := make([]*types.Var, r.uint64()) |
| 1034 | for i := range xs { |
| 1035 | xs[i] = r.param() |
| 1036 | } |
| 1037 | return types.NewTuple(xs...) |
| 1038 | } |
| 1039 | |
| 1040 | func (r *importReader) param() *types.Var { |
| 1041 | pos := r.pos() |
| 1042 | name := r.ident() |
| 1043 | typ := r.typ() |
| 1044 | return types.NewParam(pos, r.currPkg, name, typ) |
| 1045 | } |
| 1046 | |
| 1047 | func (r *importReader) bool() bool { |
| 1048 | return r.uint64() != 0 |
| 1049 | } |
| 1050 | |
| 1051 | func (r *importReader) int64() int64 { |
| 1052 | n, err := binary.ReadVarint(&r.declReader) |
| 1053 | if err != nil { |
| 1054 | errorf("readVarint: %v", err) |
| 1055 | } |
| 1056 | return n |
| 1057 | } |
| 1058 | |
| 1059 | func (r *importReader) uint64() uint64 { |
| 1060 | n, err := binary.ReadUvarint(&r.declReader) |
| 1061 | if err != nil { |
| 1062 | errorf("readUvarint: %v", err) |
| 1063 | } |
| 1064 | return n |
| 1065 | } |
| 1066 | |
| 1067 | func (r *importReader) byte() byte { |
| 1068 | x, err := r.declReader.ReadByte() |
| 1069 | if err != nil { |
| 1070 | errorf("declReader.ReadByte: %v", err) |
| 1071 | } |
| 1072 | return x |
| 1073 | } |
Robert Findley | 0cffec9 | 2021-09-14 13:07:03 -0400 | [diff] [blame] | 1074 | |
| 1075 | func baseType(typ types.Type) *types.Named { |
| 1076 | // pointer receivers are never types.Named types |
| 1077 | if p, _ := typ.(*types.Pointer); p != nil { |
| 1078 | typ = p.Elem() |
| 1079 | } |
| 1080 | // receiver base types are always (possibly generic) types.Named types |
| 1081 | n, _ := typ.(*types.Named) |
| 1082 | return n |
| 1083 | } |