]> Cypherpunks.ru repositories - gostls13.git/blob - src/cmd/compile/internal/noder/writer.go
cmd/compile/internal/noder: stop preserving original const strings
[gostls13.git] / src / cmd / compile / internal / noder / writer.go
1 // Copyright 2021 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 package noder
6
7 import (
8         "fmt"
9         "go/constant"
10         "go/token"
11         "internal/buildcfg"
12         "internal/pkgbits"
13
14         "cmd/compile/internal/base"
15         "cmd/compile/internal/ir"
16         "cmd/compile/internal/syntax"
17         "cmd/compile/internal/types"
18         "cmd/compile/internal/types2"
19 )
20
21 // This file implements the Unified IR package writer and defines the
22 // Unified IR export data format.
23 //
24 // Low-level coding details (e.g., byte-encoding of individual
25 // primitive values, or handling element bitstreams and
26 // cross-references) are handled by internal/pkgbits, so here we only
27 // concern ourselves with higher-level worries like mapping Go
28 // language constructs into elements.
29
30 // There are two central types in the writing process: the "writer"
31 // type handles writing out individual elements, while the "pkgWriter"
32 // type keeps track of which elements have already been created.
33 //
34 // For each sort of "thing" (e.g., position, package, object, type)
35 // that can be written into the export data, there are generally
36 // several methods that work together:
37 //
38 // - writer.thing handles writing out a *use* of a thing, which often
39 //   means writing a relocation to that thing's encoded index.
40 //
41 // - pkgWriter.thingIdx handles reserving an index for a thing, and
42 //   writing out any elements needed for the thing.
43 //
44 // - writer.doThing handles writing out the *definition* of a thing,
45 //   which in general is a mix of low-level coding primitives (e.g.,
46 //   ints and strings) or uses of other things.
47 //
48 // A design goal of Unified IR is to have a single, canonical writer
49 // implementation, but multiple reader implementations each tailored
50 // to their respective needs. For example, within cmd/compile's own
51 // backend, inlining is implemented largely by just re-running the
52 // function body reading code.
53
54 // TODO(mdempsky): Add an importer for Unified IR to the x/tools repo,
55 // and better document the file format boundary between public and
56 // private data.
57
58 // A pkgWriter constructs Unified IR export data from the results of
59 // running the types2 type checker on a Go compilation unit.
60 type pkgWriter struct {
61         pkgbits.PkgEncoder
62
63         m      posMap
64         curpkg *types2.Package
65         info   *types2.Info
66
67         // Indices for previously written syntax and types2 things.
68
69         posBasesIdx map[*syntax.PosBase]pkgbits.Index
70         pkgsIdx     map[*types2.Package]pkgbits.Index
71         typsIdx     map[types2.Type]pkgbits.Index
72         objsIdx     map[types2.Object]pkgbits.Index
73
74         // Maps from types2.Objects back to their syntax.Decl.
75
76         funDecls map[*types2.Func]*syntax.FuncDecl
77         typDecls map[*types2.TypeName]typeDeclGen
78
79         // linknames maps package-scope objects to their linker symbol name,
80         // if specified by a //go:linkname directive.
81         linknames map[types2.Object]string
82
83         // cgoPragmas accumulates any //go:cgo_* pragmas that need to be
84         // passed through to cmd/link.
85         cgoPragmas [][]string
86 }
87
88 // newPkgWriter returns an initialized pkgWriter for the specified
89 // package.
90 func newPkgWriter(m posMap, pkg *types2.Package, info *types2.Info) *pkgWriter {
91         return &pkgWriter{
92                 PkgEncoder: pkgbits.NewPkgEncoder(base.Debug.SyncFrames),
93
94                 m:      m,
95                 curpkg: pkg,
96                 info:   info,
97
98                 pkgsIdx: make(map[*types2.Package]pkgbits.Index),
99                 objsIdx: make(map[types2.Object]pkgbits.Index),
100                 typsIdx: make(map[types2.Type]pkgbits.Index),
101
102                 posBasesIdx: make(map[*syntax.PosBase]pkgbits.Index),
103
104                 funDecls: make(map[*types2.Func]*syntax.FuncDecl),
105                 typDecls: make(map[*types2.TypeName]typeDeclGen),
106
107                 linknames: make(map[types2.Object]string),
108         }
109 }
110
111 // errorf reports a user error about thing p.
112 func (pw *pkgWriter) errorf(p poser, msg string, args ...interface{}) {
113         base.ErrorfAt(pw.m.pos(p), 0, msg, args...)
114 }
115
116 // fatalf reports an internal compiler error about thing p.
117 func (pw *pkgWriter) fatalf(p poser, msg string, args ...interface{}) {
118         base.FatalfAt(pw.m.pos(p), msg, args...)
119 }
120
121 // unexpected reports a fatal error about a thing of unexpected
122 // dynamic type.
123 func (pw *pkgWriter) unexpected(what string, p poser) {
124         pw.fatalf(p, "unexpected %s: %v (%T)", what, p, p)
125 }
126
127 func (pw *pkgWriter) typeAndValue(x syntax.Expr) syntax.TypeAndValue {
128         tv, ok := pw.maybeTypeAndValue(x)
129         if !ok {
130                 pw.fatalf(x, "missing Types entry: %v", syntax.String(x))
131         }
132         return tv
133 }
134
135 func (pw *pkgWriter) maybeTypeAndValue(x syntax.Expr) (syntax.TypeAndValue, bool) {
136         tv := x.GetTypeInfo()
137
138         // If x is a generic function whose type arguments are inferred
139         // from assignment context, then we need to find its inferred type
140         // in Info.Instances instead.
141         if name, ok := x.(*syntax.Name); ok {
142                 if inst, ok := pw.info.Instances[name]; ok {
143                         tv.Type = inst.Type
144                 }
145         }
146
147         return tv, tv.Type != nil
148 }
149
150 // typeOf returns the Type of the given value expression.
151 func (pw *pkgWriter) typeOf(expr syntax.Expr) types2.Type {
152         tv := pw.typeAndValue(expr)
153         if !tv.IsValue() {
154                 pw.fatalf(expr, "expected value: %v", syntax.String(expr))
155         }
156         return tv.Type
157 }
158
159 // A writer provides APIs for writing out an individual element.
160 type writer struct {
161         p *pkgWriter
162
163         pkgbits.Encoder
164
165         // sig holds the signature for the current function body, if any.
166         sig *types2.Signature
167
168         // TODO(mdempsky): We should be able to prune localsIdx whenever a
169         // scope closes, and then maybe we can just use the same map for
170         // storing the TypeParams too (as their TypeName instead).
171
172         // localsIdx tracks any local variables declared within this
173         // function body. It's unused for writing out non-body things.
174         localsIdx map[*types2.Var]int
175
176         // closureVars tracks any free variables that are referenced by this
177         // function body. It's unused for writing out non-body things.
178         closureVars    []posVar
179         closureVarsIdx map[*types2.Var]int // index of previously seen free variables
180
181         dict *writerDict
182
183         // derived tracks whether the type being written out references any
184         // type parameters. It's unused for writing non-type things.
185         derived bool
186 }
187
188 // A writerDict tracks types and objects that are used by a declaration.
189 type writerDict struct {
190         implicits []*types2.TypeName
191
192         // derived is a slice of type indices for computing derived types
193         // (i.e., types that depend on the declaration's type parameters).
194         derived []derivedInfo
195
196         // derivedIdx maps a Type to its corresponding index within the
197         // derived slice, if present.
198         derivedIdx map[types2.Type]pkgbits.Index
199
200         // These slices correspond to entries in the runtime dictionary.
201         typeParamMethodExprs []writerMethodExprInfo
202         subdicts             []objInfo
203         rtypes               []typeInfo
204         itabs                []itabInfo
205 }
206
207 type itabInfo struct {
208         typ   typeInfo
209         iface typeInfo
210 }
211
212 // typeParamIndex returns the index of the given type parameter within
213 // the dictionary. This may differ from typ.Index() when there are
214 // implicit type parameters due to defined types declared within a
215 // generic function or method.
216 func (dict *writerDict) typeParamIndex(typ *types2.TypeParam) int {
217         for idx, implicit := range dict.implicits {
218                 if implicit.Type().(*types2.TypeParam) == typ {
219                         return idx
220                 }
221         }
222
223         return len(dict.implicits) + typ.Index()
224 }
225
226 // A derivedInfo represents a reference to an encoded generic Go type.
227 type derivedInfo struct {
228         idx    pkgbits.Index
229         needed bool // TODO(mdempsky): Remove.
230 }
231
232 // A typeInfo represents a reference to an encoded Go type.
233 //
234 // If derived is true, then the typeInfo represents a generic Go type
235 // that contains type parameters. In this case, idx is an index into
236 // the readerDict.derived{,Types} arrays.
237 //
238 // Otherwise, the typeInfo represents a non-generic Go type, and idx
239 // is an index into the reader.typs array instead.
240 type typeInfo struct {
241         idx     pkgbits.Index
242         derived bool
243 }
244
245 // An objInfo represents a reference to an encoded, instantiated (if
246 // applicable) Go object.
247 type objInfo struct {
248         idx       pkgbits.Index // index for the generic function declaration
249         explicits []typeInfo    // info for the type arguments
250 }
251
252 // A selectorInfo represents a reference to an encoded field or method
253 // name (i.e., objects that can only be accessed using selector
254 // expressions).
255 type selectorInfo struct {
256         pkgIdx  pkgbits.Index
257         nameIdx pkgbits.Index
258 }
259
260 // anyDerived reports whether any of info's explicit type arguments
261 // are derived types.
262 func (info objInfo) anyDerived() bool {
263         for _, explicit := range info.explicits {
264                 if explicit.derived {
265                         return true
266                 }
267         }
268         return false
269 }
270
271 // equals reports whether info and other represent the same Go object
272 // (i.e., same base object and identical type arguments, if any).
273 func (info objInfo) equals(other objInfo) bool {
274         if info.idx != other.idx {
275                 return false
276         }
277         assert(len(info.explicits) == len(other.explicits))
278         for i, targ := range info.explicits {
279                 if targ != other.explicits[i] {
280                         return false
281                 }
282         }
283         return true
284 }
285
286 type writerMethodExprInfo struct {
287         typeParamIdx int
288         methodInfo   selectorInfo
289 }
290
291 // typeParamMethodExprIdx returns the index where the given encoded
292 // method expression function pointer appears within this dictionary's
293 // type parameters method expressions section, adding it if necessary.
294 func (dict *writerDict) typeParamMethodExprIdx(typeParamIdx int, methodInfo selectorInfo) int {
295         newInfo := writerMethodExprInfo{typeParamIdx, methodInfo}
296
297         for idx, oldInfo := range dict.typeParamMethodExprs {
298                 if oldInfo == newInfo {
299                         return idx
300                 }
301         }
302
303         idx := len(dict.typeParamMethodExprs)
304         dict.typeParamMethodExprs = append(dict.typeParamMethodExprs, newInfo)
305         return idx
306 }
307
308 // subdictIdx returns the index where the given encoded object's
309 // runtime dictionary appears within this dictionary's subdictionary
310 // section, adding it if necessary.
311 func (dict *writerDict) subdictIdx(newInfo objInfo) int {
312         for idx, oldInfo := range dict.subdicts {
313                 if oldInfo.equals(newInfo) {
314                         return idx
315                 }
316         }
317
318         idx := len(dict.subdicts)
319         dict.subdicts = append(dict.subdicts, newInfo)
320         return idx
321 }
322
323 // rtypeIdx returns the index where the given encoded type's
324 // *runtime._type value appears within this dictionary's rtypes
325 // section, adding it if necessary.
326 func (dict *writerDict) rtypeIdx(newInfo typeInfo) int {
327         for idx, oldInfo := range dict.rtypes {
328                 if oldInfo == newInfo {
329                         return idx
330                 }
331         }
332
333         idx := len(dict.rtypes)
334         dict.rtypes = append(dict.rtypes, newInfo)
335         return idx
336 }
337
338 // itabIdx returns the index where the given encoded type pair's
339 // *runtime.itab value appears within this dictionary's itabs section,
340 // adding it if necessary.
341 func (dict *writerDict) itabIdx(typInfo, ifaceInfo typeInfo) int {
342         newInfo := itabInfo{typInfo, ifaceInfo}
343
344         for idx, oldInfo := range dict.itabs {
345                 if oldInfo == newInfo {
346                         return idx
347                 }
348         }
349
350         idx := len(dict.itabs)
351         dict.itabs = append(dict.itabs, newInfo)
352         return idx
353 }
354
355 func (pw *pkgWriter) newWriter(k pkgbits.RelocKind, marker pkgbits.SyncMarker) *writer {
356         return &writer{
357                 Encoder: pw.NewEncoder(k, marker),
358                 p:       pw,
359         }
360 }
361
362 // @@@ Positions
363
364 // pos writes the position of p into the element bitstream.
365 func (w *writer) pos(p poser) {
366         w.Sync(pkgbits.SyncPos)
367         pos := p.Pos()
368
369         // TODO(mdempsky): Track down the remaining cases here and fix them.
370         if !w.Bool(pos.IsKnown()) {
371                 return
372         }
373
374         // TODO(mdempsky): Delta encoding.
375         w.posBase(pos.Base())
376         w.Uint(pos.Line())
377         w.Uint(pos.Col())
378 }
379
380 // posBase writes a reference to the given PosBase into the element
381 // bitstream.
382 func (w *writer) posBase(b *syntax.PosBase) {
383         w.Reloc(pkgbits.RelocPosBase, w.p.posBaseIdx(b))
384 }
385
386 // posBaseIdx returns the index for the given PosBase.
387 func (pw *pkgWriter) posBaseIdx(b *syntax.PosBase) pkgbits.Index {
388         if idx, ok := pw.posBasesIdx[b]; ok {
389                 return idx
390         }
391
392         w := pw.newWriter(pkgbits.RelocPosBase, pkgbits.SyncPosBase)
393         w.p.posBasesIdx[b] = w.Idx
394
395         w.String(trimFilename(b))
396
397         if !w.Bool(b.IsFileBase()) {
398                 w.pos(b)
399                 w.Uint(b.Line())
400                 w.Uint(b.Col())
401         }
402
403         return w.Flush()
404 }
405
406 // @@@ Packages
407
408 // pkg writes a use of the given Package into the element bitstream.
409 func (w *writer) pkg(pkg *types2.Package) {
410         w.pkgRef(w.p.pkgIdx(pkg))
411 }
412
413 func (w *writer) pkgRef(idx pkgbits.Index) {
414         w.Sync(pkgbits.SyncPkg)
415         w.Reloc(pkgbits.RelocPkg, idx)
416 }
417
418 // pkgIdx returns the index for the given package, adding it to the
419 // package export data if needed.
420 func (pw *pkgWriter) pkgIdx(pkg *types2.Package) pkgbits.Index {
421         if idx, ok := pw.pkgsIdx[pkg]; ok {
422                 return idx
423         }
424
425         w := pw.newWriter(pkgbits.RelocPkg, pkgbits.SyncPkgDef)
426         pw.pkgsIdx[pkg] = w.Idx
427
428         // The universe and package unsafe need to be handled specially by
429         // importers anyway, so we serialize them using just their package
430         // path. This ensures that readers don't confuse them for
431         // user-defined packages.
432         switch pkg {
433         case nil: // universe
434                 w.String("builtin") // same package path used by godoc
435         case types2.Unsafe:
436                 w.String("unsafe")
437         default:
438                 // TODO(mdempsky): Write out pkg.Path() for curpkg too.
439                 var path string
440                 if pkg != w.p.curpkg {
441                         path = pkg.Path()
442                 }
443                 base.Assertf(path != "builtin" && path != "unsafe", "unexpected path for user-defined package: %q", path)
444                 w.String(path)
445                 w.String(pkg.Name())
446
447                 w.Len(len(pkg.Imports()))
448                 for _, imp := range pkg.Imports() {
449                         w.pkg(imp)
450                 }
451         }
452
453         return w.Flush()
454 }
455
456 // @@@ Types
457
458 var (
459         anyTypeName        = types2.Universe.Lookup("any").(*types2.TypeName)
460         comparableTypeName = types2.Universe.Lookup("comparable").(*types2.TypeName)
461         runeTypeName       = types2.Universe.Lookup("rune").(*types2.TypeName)
462 )
463
464 // typ writes a use of the given type into the bitstream.
465 func (w *writer) typ(typ types2.Type) {
466         w.typInfo(w.p.typIdx(typ, w.dict))
467 }
468
469 // typInfo writes a use of the given type (specified as a typeInfo
470 // instead) into the bitstream.
471 func (w *writer) typInfo(info typeInfo) {
472         w.Sync(pkgbits.SyncType)
473         if w.Bool(info.derived) {
474                 w.Len(int(info.idx))
475                 w.derived = true
476         } else {
477                 w.Reloc(pkgbits.RelocType, info.idx)
478         }
479 }
480
481 // typIdx returns the index where the export data description of type
482 // can be read back in. If no such index exists yet, it's created.
483 //
484 // typIdx also reports whether typ is a derived type; that is, whether
485 // its identity depends on type parameters.
486 func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
487         if idx, ok := pw.typsIdx[typ]; ok {
488                 return typeInfo{idx: idx, derived: false}
489         }
490         if dict != nil {
491                 if idx, ok := dict.derivedIdx[typ]; ok {
492                         return typeInfo{idx: idx, derived: true}
493                 }
494         }
495
496         w := pw.newWriter(pkgbits.RelocType, pkgbits.SyncTypeIdx)
497         w.dict = dict
498
499         switch typ := typ.(type) {
500         default:
501                 base.Fatalf("unexpected type: %v (%T)", typ, typ)
502
503         case *types2.Basic:
504                 switch kind := typ.Kind(); {
505                 case kind == types2.Invalid:
506                         base.Fatalf("unexpected types2.Invalid")
507
508                 case types2.Typ[kind] == typ:
509                         w.Code(pkgbits.TypeBasic)
510                         w.Len(int(kind))
511
512                 default:
513                         // Handle "byte" and "rune" as references to their TypeNames.
514                         obj := types2.Universe.Lookup(typ.Name())
515                         assert(obj.Type() == typ)
516
517                         w.Code(pkgbits.TypeNamed)
518                         w.obj(obj, nil)
519                 }
520
521         case *types2.Named:
522                 obj, targs := splitNamed(typ)
523
524                 // Defined types that are declared within a generic function (and
525                 // thus have implicit type parameters) are always derived types.
526                 if w.p.hasImplicitTypeParams(obj) {
527                         w.derived = true
528                 }
529
530                 w.Code(pkgbits.TypeNamed)
531                 w.obj(obj, targs)
532
533         case *types2.TypeParam:
534                 w.derived = true
535                 w.Code(pkgbits.TypeTypeParam)
536                 w.Len(w.dict.typeParamIndex(typ))
537
538         case *types2.Array:
539                 w.Code(pkgbits.TypeArray)
540                 w.Uint64(uint64(typ.Len()))
541                 w.typ(typ.Elem())
542
543         case *types2.Chan:
544                 w.Code(pkgbits.TypeChan)
545                 w.Len(int(typ.Dir()))
546                 w.typ(typ.Elem())
547
548         case *types2.Map:
549                 w.Code(pkgbits.TypeMap)
550                 w.typ(typ.Key())
551                 w.typ(typ.Elem())
552
553         case *types2.Pointer:
554                 w.Code(pkgbits.TypePointer)
555                 w.typ(typ.Elem())
556
557         case *types2.Signature:
558                 base.Assertf(typ.TypeParams() == nil, "unexpected type params: %v", typ)
559                 w.Code(pkgbits.TypeSignature)
560                 w.signature(typ)
561
562         case *types2.Slice:
563                 w.Code(pkgbits.TypeSlice)
564                 w.typ(typ.Elem())
565
566         case *types2.Struct:
567                 w.Code(pkgbits.TypeStruct)
568                 w.structType(typ)
569
570         case *types2.Interface:
571                 // Handle "any" as reference to its TypeName.
572                 if typ == anyTypeName.Type() {
573                         w.Code(pkgbits.TypeNamed)
574                         w.obj(anyTypeName, nil)
575                         break
576                 }
577
578                 w.Code(pkgbits.TypeInterface)
579                 w.interfaceType(typ)
580
581         case *types2.Union:
582                 w.Code(pkgbits.TypeUnion)
583                 w.unionType(typ)
584         }
585
586         if w.derived {
587                 idx := pkgbits.Index(len(dict.derived))
588                 dict.derived = append(dict.derived, derivedInfo{idx: w.Flush()})
589                 dict.derivedIdx[typ] = idx
590                 return typeInfo{idx: idx, derived: true}
591         }
592
593         pw.typsIdx[typ] = w.Idx
594         return typeInfo{idx: w.Flush(), derived: false}
595 }
596
597 func (w *writer) structType(typ *types2.Struct) {
598         w.Len(typ.NumFields())
599         for i := 0; i < typ.NumFields(); i++ {
600                 f := typ.Field(i)
601                 w.pos(f)
602                 w.selector(f)
603                 w.typ(f.Type())
604                 w.String(typ.Tag(i))
605                 w.Bool(f.Embedded())
606         }
607 }
608
609 func (w *writer) unionType(typ *types2.Union) {
610         w.Len(typ.Len())
611         for i := 0; i < typ.Len(); i++ {
612                 t := typ.Term(i)
613                 w.Bool(t.Tilde())
614                 w.typ(t.Type())
615         }
616 }
617
618 func (w *writer) interfaceType(typ *types2.Interface) {
619         // If typ has no embedded types but it's not a basic interface, then
620         // the natural description we write out below will fail to
621         // reconstruct it.
622         if typ.NumEmbeddeds() == 0 && !typ.IsMethodSet() {
623                 // Currently, this can only happen for the underlying Interface of
624                 // "comparable", which is needed to handle type declarations like
625                 // "type C comparable".
626                 assert(typ == comparableTypeName.Type().(*types2.Named).Underlying())
627
628                 // Export as "interface{ comparable }".
629                 w.Len(0)                         // NumExplicitMethods
630                 w.Len(1)                         // NumEmbeddeds
631                 w.Bool(false)                    // IsImplicit
632                 w.typ(comparableTypeName.Type()) // EmbeddedType(0)
633                 return
634         }
635
636         w.Len(typ.NumExplicitMethods())
637         w.Len(typ.NumEmbeddeds())
638
639         if typ.NumExplicitMethods() == 0 && typ.NumEmbeddeds() == 1 {
640                 w.Bool(typ.IsImplicit())
641         } else {
642                 // Implicit interfaces always have 0 explicit methods and 1
643                 // embedded type, so we skip writing out the implicit flag
644                 // otherwise as a space optimization.
645                 assert(!typ.IsImplicit())
646         }
647
648         for i := 0; i < typ.NumExplicitMethods(); i++ {
649                 m := typ.ExplicitMethod(i)
650                 sig := m.Type().(*types2.Signature)
651                 assert(sig.TypeParams() == nil)
652
653                 w.pos(m)
654                 w.selector(m)
655                 w.signature(sig)
656         }
657
658         for i := 0; i < typ.NumEmbeddeds(); i++ {
659                 w.typ(typ.EmbeddedType(i))
660         }
661 }
662
663 func (w *writer) signature(sig *types2.Signature) {
664         w.Sync(pkgbits.SyncSignature)
665         w.params(sig.Params())
666         w.params(sig.Results())
667         w.Bool(sig.Variadic())
668 }
669
670 func (w *writer) params(typ *types2.Tuple) {
671         w.Sync(pkgbits.SyncParams)
672         w.Len(typ.Len())
673         for i := 0; i < typ.Len(); i++ {
674                 w.param(typ.At(i))
675         }
676 }
677
678 func (w *writer) param(param *types2.Var) {
679         w.Sync(pkgbits.SyncParam)
680         w.pos(param)
681         w.localIdent(param)
682         w.typ(param.Type())
683 }
684
685 // @@@ Objects
686
687 // obj writes a use of the given object into the bitstream.
688 //
689 // If obj is a generic object, then explicits are the explicit type
690 // arguments used to instantiate it (i.e., used to substitute the
691 // object's own declared type parameters).
692 func (w *writer) obj(obj types2.Object, explicits *types2.TypeList) {
693         w.objInfo(w.p.objInstIdx(obj, explicits, w.dict))
694 }
695
696 // objInfo writes a use of the given encoded object into the
697 // bitstream.
698 func (w *writer) objInfo(info objInfo) {
699         w.Sync(pkgbits.SyncObject)
700         w.Bool(false) // TODO(mdempsky): Remove; was derived func inst.
701         w.Reloc(pkgbits.RelocObj, info.idx)
702
703         w.Len(len(info.explicits))
704         for _, info := range info.explicits {
705                 w.typInfo(info)
706         }
707 }
708
709 // objInstIdx returns the indices for an object and a corresponding
710 // list of type arguments used to instantiate it, adding them to the
711 // export data as needed.
712 func (pw *pkgWriter) objInstIdx(obj types2.Object, explicits *types2.TypeList, dict *writerDict) objInfo {
713         explicitInfos := make([]typeInfo, explicits.Len())
714         for i := range explicitInfos {
715                 explicitInfos[i] = pw.typIdx(explicits.At(i), dict)
716         }
717         return objInfo{idx: pw.objIdx(obj), explicits: explicitInfos}
718 }
719
720 // objIdx returns the index for the given Object, adding it to the
721 // export data as needed.
722 func (pw *pkgWriter) objIdx(obj types2.Object) pkgbits.Index {
723         // TODO(mdempsky): Validate that obj is a global object (or a local
724         // defined type, which we hoist to global scope anyway).
725
726         if idx, ok := pw.objsIdx[obj]; ok {
727                 return idx
728         }
729
730         dict := &writerDict{
731                 derivedIdx: make(map[types2.Type]pkgbits.Index),
732         }
733
734         if isDefinedType(obj) && obj.Pkg() == pw.curpkg {
735                 decl, ok := pw.typDecls[obj.(*types2.TypeName)]
736                 assert(ok)
737                 dict.implicits = decl.implicits
738         }
739
740         // We encode objects into 4 elements across different sections, all
741         // sharing the same index:
742         //
743         // - RelocName has just the object's qualified name (i.e.,
744         //   Object.Pkg and Object.Name) and the CodeObj indicating what
745         //   specific type of Object it is (Var, Func, etc).
746         //
747         // - RelocObj has the remaining public details about the object,
748         //   relevant to go/types importers.
749         //
750         // - RelocObjExt has additional private details about the object,
751         //   which are only relevant to cmd/compile itself. This is
752         //   separated from RelocObj so that go/types importers are
753         //   unaffected by internal compiler changes.
754         //
755         // - RelocObjDict has public details about the object's type
756         //   parameters and derived type's used by the object. This is
757         //   separated to facilitate the eventual introduction of
758         //   shape-based stenciling.
759         //
760         // TODO(mdempsky): Re-evaluate whether RelocName still makes sense
761         // to keep separate from RelocObj.
762
763         w := pw.newWriter(pkgbits.RelocObj, pkgbits.SyncObject1)
764         wext := pw.newWriter(pkgbits.RelocObjExt, pkgbits.SyncObject1)
765         wname := pw.newWriter(pkgbits.RelocName, pkgbits.SyncObject1)
766         wdict := pw.newWriter(pkgbits.RelocObjDict, pkgbits.SyncObject1)
767
768         pw.objsIdx[obj] = w.Idx // break cycles
769         assert(wext.Idx == w.Idx)
770         assert(wname.Idx == w.Idx)
771         assert(wdict.Idx == w.Idx)
772
773         w.dict = dict
774         wext.dict = dict
775
776         code := w.doObj(wext, obj)
777         w.Flush()
778         wext.Flush()
779
780         wname.qualifiedIdent(obj)
781         wname.Code(code)
782         wname.Flush()
783
784         wdict.objDict(obj, w.dict)
785         wdict.Flush()
786
787         return w.Idx
788 }
789
790 // doObj writes the RelocObj definition for obj to w, and the
791 // RelocObjExt definition to wext.
792 func (w *writer) doObj(wext *writer, obj types2.Object) pkgbits.CodeObj {
793         if obj.Pkg() != w.p.curpkg {
794                 return pkgbits.ObjStub
795         }
796
797         switch obj := obj.(type) {
798         default:
799                 w.p.unexpected("object", obj)
800                 panic("unreachable")
801
802         case *types2.Const:
803                 w.pos(obj)
804                 w.typ(obj.Type())
805                 w.Value(obj.Val())
806                 return pkgbits.ObjConst
807
808         case *types2.Func:
809                 decl, ok := w.p.funDecls[obj]
810                 assert(ok)
811                 sig := obj.Type().(*types2.Signature)
812
813                 w.pos(obj)
814                 w.typeParamNames(sig.TypeParams())
815                 w.signature(sig)
816                 w.pos(decl)
817                 wext.funcExt(obj)
818                 return pkgbits.ObjFunc
819
820         case *types2.TypeName:
821                 if obj.IsAlias() {
822                         w.pos(obj)
823                         w.typ(obj.Type())
824                         return pkgbits.ObjAlias
825                 }
826
827                 named := obj.Type().(*types2.Named)
828                 assert(named.TypeArgs() == nil)
829
830                 w.pos(obj)
831                 w.typeParamNames(named.TypeParams())
832                 wext.typeExt(obj)
833                 w.typ(named.Underlying())
834
835                 w.Len(named.NumMethods())
836                 for i := 0; i < named.NumMethods(); i++ {
837                         w.method(wext, named.Method(i))
838                 }
839
840                 return pkgbits.ObjType
841
842         case *types2.Var:
843                 w.pos(obj)
844                 w.typ(obj.Type())
845                 wext.varExt(obj)
846                 return pkgbits.ObjVar
847         }
848 }
849
850 // objDict writes the dictionary needed for reading the given object.
851 func (w *writer) objDict(obj types2.Object, dict *writerDict) {
852         // TODO(mdempsky): Split objDict into multiple entries? reader.go
853         // doesn't care about the type parameter bounds, and reader2.go
854         // doesn't care about referenced functions.
855
856         w.dict = dict // TODO(mdempsky): This is a bit sketchy.
857
858         w.Len(len(dict.implicits))
859
860         tparams := objTypeParams(obj)
861         ntparams := tparams.Len()
862         w.Len(ntparams)
863         for i := 0; i < ntparams; i++ {
864                 w.typ(tparams.At(i).Constraint())
865         }
866
867         nderived := len(dict.derived)
868         w.Len(nderived)
869         for _, typ := range dict.derived {
870                 w.Reloc(pkgbits.RelocType, typ.idx)
871                 w.Bool(typ.needed)
872         }
873
874         // Write runtime dictionary information.
875         //
876         // N.B., the go/types importer reads up to the section, but doesn't
877         // read any further, so it's safe to change. (See TODO above.)
878
879         // For each type parameter, write out whether the constraint is a
880         // basic interface. This is used to determine how aggressively we
881         // can shape corresponding type arguments.
882         //
883         // This is somewhat redundant with writing out the full type
884         // parameter constraints above, but the compiler currently skips
885         // over those. Also, we don't care about the *declared* constraints,
886         // but how the type parameters are actually *used*. E.g., if a type
887         // parameter is constrained to `int | uint` but then never used in
888         // arithmetic/conversions/etc, we could shape those together.
889         for _, implicit := range dict.implicits {
890                 tparam := implicit.Type().(*types2.TypeParam)
891                 w.Bool(tparam.Underlying().(*types2.Interface).IsMethodSet())
892         }
893         for i := 0; i < ntparams; i++ {
894                 tparam := tparams.At(i)
895                 w.Bool(tparam.Underlying().(*types2.Interface).IsMethodSet())
896         }
897
898         w.Len(len(dict.typeParamMethodExprs))
899         for _, info := range dict.typeParamMethodExprs {
900                 w.Len(info.typeParamIdx)
901                 w.selectorInfo(info.methodInfo)
902         }
903
904         w.Len(len(dict.subdicts))
905         for _, info := range dict.subdicts {
906                 w.objInfo(info)
907         }
908
909         w.Len(len(dict.rtypes))
910         for _, info := range dict.rtypes {
911                 w.typInfo(info)
912         }
913
914         w.Len(len(dict.itabs))
915         for _, info := range dict.itabs {
916                 w.typInfo(info.typ)
917                 w.typInfo(info.iface)
918         }
919
920         assert(len(dict.derived) == nderived)
921 }
922
923 func (w *writer) typeParamNames(tparams *types2.TypeParamList) {
924         w.Sync(pkgbits.SyncTypeParamNames)
925
926         ntparams := tparams.Len()
927         for i := 0; i < ntparams; i++ {
928                 tparam := tparams.At(i).Obj()
929                 w.pos(tparam)
930                 w.localIdent(tparam)
931         }
932 }
933
934 func (w *writer) method(wext *writer, meth *types2.Func) {
935         decl, ok := w.p.funDecls[meth]
936         assert(ok)
937         sig := meth.Type().(*types2.Signature)
938
939         w.Sync(pkgbits.SyncMethod)
940         w.pos(meth)
941         w.selector(meth)
942         w.typeParamNames(sig.RecvTypeParams())
943         w.param(sig.Recv())
944         w.signature(sig)
945
946         w.pos(decl) // XXX: Hack to workaround linker limitations.
947         wext.funcExt(meth)
948 }
949
950 // qualifiedIdent writes out the name of an object declared at package
951 // scope. (For now, it's also used to refer to local defined types.)
952 func (w *writer) qualifiedIdent(obj types2.Object) {
953         w.Sync(pkgbits.SyncSym)
954
955         name := obj.Name()
956         if isDefinedType(obj) && obj.Pkg() == w.p.curpkg {
957                 decl, ok := w.p.typDecls[obj.(*types2.TypeName)]
958                 assert(ok)
959                 if decl.gen != 0 {
960                         // For local defined types, we embed a scope-disambiguation
961                         // number directly into their name. types.SplitVargenSuffix then
962                         // knows to look for this.
963                         //
964                         // TODO(mdempsky): Find a better solution; this is terrible.
965                         name = fmt.Sprintf("%s·%v", name, decl.gen)
966                 }
967         }
968
969         w.pkg(obj.Pkg())
970         w.String(name)
971 }
972
973 // TODO(mdempsky): We should be able to omit pkg from both localIdent
974 // and selector, because they should always be known from context.
975 // However, past frustrations with this optimization in iexport make
976 // me a little nervous to try it again.
977
978 // localIdent writes the name of a locally declared object (i.e.,
979 // objects that can only be accessed by non-qualified name, within the
980 // context of a particular function).
981 func (w *writer) localIdent(obj types2.Object) {
982         assert(!isGlobal(obj))
983         w.Sync(pkgbits.SyncLocalIdent)
984         w.pkg(obj.Pkg())
985         w.String(obj.Name())
986 }
987
988 // selector writes the name of a field or method (i.e., objects that
989 // can only be accessed using selector expressions).
990 func (w *writer) selector(obj types2.Object) {
991         w.selectorInfo(w.p.selectorIdx(obj))
992 }
993
994 func (w *writer) selectorInfo(info selectorInfo) {
995         w.Sync(pkgbits.SyncSelector)
996         w.pkgRef(info.pkgIdx)
997         w.StringRef(info.nameIdx)
998 }
999
1000 func (pw *pkgWriter) selectorIdx(obj types2.Object) selectorInfo {
1001         pkgIdx := pw.pkgIdx(obj.Pkg())
1002         nameIdx := pw.StringIdx(obj.Name())
1003         return selectorInfo{pkgIdx: pkgIdx, nameIdx: nameIdx}
1004 }
1005
1006 // @@@ Compiler extensions
1007
1008 func (w *writer) funcExt(obj *types2.Func) {
1009         decl, ok := w.p.funDecls[obj]
1010         assert(ok)
1011
1012         // TODO(mdempsky): Extend these pragma validation flags to account
1013         // for generics. E.g., linkname probably doesn't make sense at
1014         // least.
1015
1016         pragma := asPragmaFlag(decl.Pragma)
1017         if pragma&ir.Systemstack != 0 && pragma&ir.Nosplit != 0 {
1018                 w.p.errorf(decl, "go:nosplit and go:systemstack cannot be combined")
1019         }
1020         wi := asWasmImport(decl.Pragma)
1021
1022         if decl.Body != nil {
1023                 if pragma&ir.Noescape != 0 {
1024                         w.p.errorf(decl, "can only use //go:noescape with external func implementations")
1025                 }
1026                 if wi != nil {
1027                         w.p.errorf(decl, "can only use //go:wasmimport with external func implementations")
1028                 }
1029                 if (pragma&ir.UintptrKeepAlive != 0 && pragma&ir.UintptrEscapes == 0) && pragma&ir.Nosplit == 0 {
1030                         // Stack growth can't handle uintptr arguments that may
1031                         // be pointers (as we don't know which are pointers
1032                         // when creating the stack map). Thus uintptrkeepalive
1033                         // functions (and all transitive callees) must be
1034                         // nosplit.
1035                         //
1036                         // N.B. uintptrescapes implies uintptrkeepalive but it
1037                         // is OK since the arguments must escape to the heap.
1038                         //
1039                         // TODO(prattmic): Add recursive nosplit check of callees.
1040                         // TODO(prattmic): Functions with no body (i.e.,
1041                         // assembly) must also be nosplit, but we can't check
1042                         // that here.
1043                         w.p.errorf(decl, "go:uintptrkeepalive requires go:nosplit")
1044                 }
1045         } else {
1046                 if base.Flag.Complete || decl.Name.Value == "init" {
1047                         // Linknamed functions are allowed to have no body. Hopefully
1048                         // the linkname target has a body. See issue 23311.
1049                         // Wasmimport functions are also allowed to have no body.
1050                         if _, ok := w.p.linknames[obj]; !ok && wi == nil {
1051                                 w.p.errorf(decl, "missing function body")
1052                         }
1053                 }
1054         }
1055
1056         sig, block := obj.Type().(*types2.Signature), decl.Body
1057         body, closureVars := w.p.bodyIdx(sig, block, w.dict)
1058         assert(len(closureVars) == 0)
1059
1060         w.Sync(pkgbits.SyncFuncExt)
1061         w.pragmaFlag(pragma)
1062         w.linkname(obj)
1063
1064         if buildcfg.GOARCH == "wasm" {
1065                 if wi != nil {
1066                         w.String(wi.Module)
1067                         w.String(wi.Name)
1068                 } else {
1069                         w.String("")
1070                         w.String("")
1071                 }
1072         }
1073
1074         w.Bool(false) // stub extension
1075         w.Reloc(pkgbits.RelocBody, body)
1076         w.Sync(pkgbits.SyncEOF)
1077 }
1078
1079 func (w *writer) typeExt(obj *types2.TypeName) {
1080         decl, ok := w.p.typDecls[obj]
1081         assert(ok)
1082
1083         w.Sync(pkgbits.SyncTypeExt)
1084
1085         w.pragmaFlag(asPragmaFlag(decl.Pragma))
1086
1087         // No LSym.SymIdx info yet.
1088         w.Int64(-1)
1089         w.Int64(-1)
1090 }
1091
1092 func (w *writer) varExt(obj *types2.Var) {
1093         w.Sync(pkgbits.SyncVarExt)
1094         w.linkname(obj)
1095 }
1096
1097 func (w *writer) linkname(obj types2.Object) {
1098         w.Sync(pkgbits.SyncLinkname)
1099         w.Int64(-1)
1100         w.String(w.p.linknames[obj])
1101 }
1102
1103 func (w *writer) pragmaFlag(p ir.PragmaFlag) {
1104         w.Sync(pkgbits.SyncPragma)
1105         w.Int(int(p))
1106 }
1107
1108 // @@@ Function bodies
1109
1110 // bodyIdx returns the index for the given function body (specified by
1111 // block), adding it to the export data
1112 func (pw *pkgWriter) bodyIdx(sig *types2.Signature, block *syntax.BlockStmt, dict *writerDict) (idx pkgbits.Index, closureVars []posVar) {
1113         w := pw.newWriter(pkgbits.RelocBody, pkgbits.SyncFuncBody)
1114         w.sig = sig
1115         w.dict = dict
1116
1117         w.funcargs(sig)
1118         if w.Bool(block != nil) {
1119                 w.stmts(block.List)
1120                 w.pos(block.Rbrace)
1121         }
1122
1123         return w.Flush(), w.closureVars
1124 }
1125
1126 func (w *writer) funcargs(sig *types2.Signature) {
1127         do := func(params *types2.Tuple, result bool) {
1128                 for i := 0; i < params.Len(); i++ {
1129                         w.funcarg(params.At(i), result)
1130                 }
1131         }
1132
1133         if recv := sig.Recv(); recv != nil {
1134                 w.funcarg(recv, false)
1135         }
1136         do(sig.Params(), false)
1137         do(sig.Results(), true)
1138 }
1139
1140 func (w *writer) funcarg(param *types2.Var, result bool) {
1141         if param.Name() != "" || result {
1142                 w.addLocal(param)
1143         }
1144 }
1145
1146 // addLocal records the declaration of a new local variable.
1147 func (w *writer) addLocal(obj *types2.Var) {
1148         idx := len(w.localsIdx)
1149
1150         w.Sync(pkgbits.SyncAddLocal)
1151         if w.p.SyncMarkers() {
1152                 w.Int(idx)
1153         }
1154         w.varDictIndex(obj)
1155
1156         if w.localsIdx == nil {
1157                 w.localsIdx = make(map[*types2.Var]int)
1158         }
1159         w.localsIdx[obj] = idx
1160 }
1161
1162 // useLocal writes a reference to the given local or free variable
1163 // into the bitstream.
1164 func (w *writer) useLocal(pos syntax.Pos, obj *types2.Var) {
1165         w.Sync(pkgbits.SyncUseObjLocal)
1166
1167         if idx, ok := w.localsIdx[obj]; w.Bool(ok) {
1168                 w.Len(idx)
1169                 return
1170         }
1171
1172         idx, ok := w.closureVarsIdx[obj]
1173         if !ok {
1174                 if w.closureVarsIdx == nil {
1175                         w.closureVarsIdx = make(map[*types2.Var]int)
1176                 }
1177                 idx = len(w.closureVars)
1178                 w.closureVars = append(w.closureVars, posVar{pos, obj})
1179                 w.closureVarsIdx[obj] = idx
1180         }
1181         w.Len(idx)
1182 }
1183
1184 func (w *writer) openScope(pos syntax.Pos) {
1185         w.Sync(pkgbits.SyncOpenScope)
1186         w.pos(pos)
1187 }
1188
1189 func (w *writer) closeScope(pos syntax.Pos) {
1190         w.Sync(pkgbits.SyncCloseScope)
1191         w.pos(pos)
1192         w.closeAnotherScope()
1193 }
1194
1195 func (w *writer) closeAnotherScope() {
1196         w.Sync(pkgbits.SyncCloseAnotherScope)
1197 }
1198
1199 // @@@ Statements
1200
1201 // stmt writes the given statement into the function body bitstream.
1202 func (w *writer) stmt(stmt syntax.Stmt) {
1203         var stmts []syntax.Stmt
1204         if stmt != nil {
1205                 stmts = []syntax.Stmt{stmt}
1206         }
1207         w.stmts(stmts)
1208 }
1209
1210 func (w *writer) stmts(stmts []syntax.Stmt) {
1211         dead := false
1212         w.Sync(pkgbits.SyncStmts)
1213         for _, stmt := range stmts {
1214                 if dead {
1215                         // Any statements after a terminating statement are safe to
1216                         // omit, at least until the next labeled statement.
1217                         if _, ok := stmt.(*syntax.LabeledStmt); !ok {
1218                                 continue
1219                         }
1220                 }
1221                 w.stmt1(stmt)
1222                 dead = w.p.terminates(stmt)
1223         }
1224         w.Code(stmtEnd)
1225         w.Sync(pkgbits.SyncStmtsEnd)
1226 }
1227
1228 func (w *writer) stmt1(stmt syntax.Stmt) {
1229         switch stmt := stmt.(type) {
1230         default:
1231                 w.p.unexpected("statement", stmt)
1232
1233         case nil, *syntax.EmptyStmt:
1234                 return
1235
1236         case *syntax.AssignStmt:
1237                 switch {
1238                 case stmt.Rhs == nil:
1239                         w.Code(stmtIncDec)
1240                         w.op(binOps[stmt.Op])
1241                         w.expr(stmt.Lhs)
1242                         w.pos(stmt)
1243
1244                 case stmt.Op != 0 && stmt.Op != syntax.Def:
1245                         w.Code(stmtAssignOp)
1246                         w.op(binOps[stmt.Op])
1247                         w.expr(stmt.Lhs)
1248                         w.pos(stmt)
1249
1250                         var typ types2.Type
1251                         if stmt.Op != syntax.Shl && stmt.Op != syntax.Shr {
1252                                 typ = w.p.typeOf(stmt.Lhs)
1253                         }
1254                         w.implicitConvExpr(typ, stmt.Rhs)
1255
1256                 default:
1257                         w.assignStmt(stmt, stmt.Lhs, stmt.Rhs)
1258                 }
1259
1260         case *syntax.BlockStmt:
1261                 w.Code(stmtBlock)
1262                 w.blockStmt(stmt)
1263
1264         case *syntax.BranchStmt:
1265                 w.Code(stmtBranch)
1266                 w.pos(stmt)
1267                 w.op(branchOps[stmt.Tok])
1268                 w.optLabel(stmt.Label)
1269
1270         case *syntax.CallStmt:
1271                 w.Code(stmtCall)
1272                 w.pos(stmt)
1273                 w.op(callOps[stmt.Tok])
1274                 w.expr(stmt.Call)
1275
1276         case *syntax.DeclStmt:
1277                 for _, decl := range stmt.DeclList {
1278                         w.declStmt(decl)
1279                 }
1280
1281         case *syntax.ExprStmt:
1282                 w.Code(stmtExpr)
1283                 w.expr(stmt.X)
1284
1285         case *syntax.ForStmt:
1286                 w.Code(stmtFor)
1287                 w.forStmt(stmt)
1288
1289         case *syntax.IfStmt:
1290                 w.Code(stmtIf)
1291                 w.ifStmt(stmt)
1292
1293         case *syntax.LabeledStmt:
1294                 w.Code(stmtLabel)
1295                 w.pos(stmt)
1296                 w.label(stmt.Label)
1297                 w.stmt1(stmt.Stmt)
1298
1299         case *syntax.ReturnStmt:
1300                 w.Code(stmtReturn)
1301                 w.pos(stmt)
1302
1303                 resultTypes := w.sig.Results()
1304                 dstType := func(i int) types2.Type {
1305                         return resultTypes.At(i).Type()
1306                 }
1307                 w.multiExpr(stmt, dstType, syntax.UnpackListExpr(stmt.Results))
1308
1309         case *syntax.SelectStmt:
1310                 w.Code(stmtSelect)
1311                 w.selectStmt(stmt)
1312
1313         case *syntax.SendStmt:
1314                 chanType := types2.CoreType(w.p.typeOf(stmt.Chan)).(*types2.Chan)
1315
1316                 w.Code(stmtSend)
1317                 w.pos(stmt)
1318                 w.expr(stmt.Chan)
1319                 w.implicitConvExpr(chanType.Elem(), stmt.Value)
1320
1321         case *syntax.SwitchStmt:
1322                 w.Code(stmtSwitch)
1323                 w.switchStmt(stmt)
1324         }
1325 }
1326
1327 func (w *writer) assignList(expr syntax.Expr) {
1328         exprs := syntax.UnpackListExpr(expr)
1329         w.Len(len(exprs))
1330
1331         for _, expr := range exprs {
1332                 w.assign(expr)
1333         }
1334 }
1335
1336 func (w *writer) assign(expr syntax.Expr) {
1337         expr = syntax.Unparen(expr)
1338
1339         if name, ok := expr.(*syntax.Name); ok {
1340                 if name.Value == "_" {
1341                         w.Code(assignBlank)
1342                         return
1343                 }
1344
1345                 if obj, ok := w.p.info.Defs[name]; ok {
1346                         obj := obj.(*types2.Var)
1347
1348                         w.Code(assignDef)
1349                         w.pos(obj)
1350                         w.localIdent(obj)
1351                         w.typ(obj.Type())
1352
1353                         // TODO(mdempsky): Minimize locals index size by deferring
1354                         // this until the variables actually come into scope.
1355                         w.addLocal(obj)
1356                         return
1357                 }
1358         }
1359
1360         w.Code(assignExpr)
1361         w.expr(expr)
1362 }
1363
1364 func (w *writer) declStmt(decl syntax.Decl) {
1365         switch decl := decl.(type) {
1366         default:
1367                 w.p.unexpected("declaration", decl)
1368
1369         case *syntax.ConstDecl, *syntax.TypeDecl:
1370
1371         case *syntax.VarDecl:
1372                 w.assignStmt(decl, namesAsExpr(decl.NameList), decl.Values)
1373         }
1374 }
1375
1376 // assignStmt writes out an assignment for "lhs = rhs".
1377 func (w *writer) assignStmt(pos poser, lhs0, rhs0 syntax.Expr) {
1378         lhs := syntax.UnpackListExpr(lhs0)
1379         rhs := syntax.UnpackListExpr(rhs0)
1380
1381         w.Code(stmtAssign)
1382         w.pos(pos)
1383
1384         // As if w.assignList(lhs0).
1385         w.Len(len(lhs))
1386         for _, expr := range lhs {
1387                 w.assign(expr)
1388         }
1389
1390         dstType := func(i int) types2.Type {
1391                 dst := lhs[i]
1392
1393                 // Finding dstType is somewhat involved, because for VarDecl
1394                 // statements, the Names are only added to the info.{Defs,Uses}
1395                 // maps, not to info.Types.
1396                 if name, ok := syntax.Unparen(dst).(*syntax.Name); ok {
1397                         if name.Value == "_" {
1398                                 return nil // ok: no implicit conversion
1399                         } else if def, ok := w.p.info.Defs[name].(*types2.Var); ok {
1400                                 return def.Type()
1401                         } else if use, ok := w.p.info.Uses[name].(*types2.Var); ok {
1402                                 return use.Type()
1403                         } else {
1404                                 w.p.fatalf(dst, "cannot find type of destination object: %v", dst)
1405                         }
1406                 }
1407
1408                 return w.p.typeOf(dst)
1409         }
1410
1411         w.multiExpr(pos, dstType, rhs)
1412 }
1413
1414 func (w *writer) blockStmt(stmt *syntax.BlockStmt) {
1415         w.Sync(pkgbits.SyncBlockStmt)
1416         w.openScope(stmt.Pos())
1417         w.stmts(stmt.List)
1418         w.closeScope(stmt.Rbrace)
1419 }
1420
1421 func (w *writer) forStmt(stmt *syntax.ForStmt) {
1422         w.Sync(pkgbits.SyncForStmt)
1423         w.openScope(stmt.Pos())
1424
1425         if rang, ok := stmt.Init.(*syntax.RangeClause); w.Bool(ok) {
1426                 w.pos(rang)
1427                 w.assignList(rang.Lhs)
1428                 w.expr(rang.X)
1429
1430                 xtyp := w.p.typeOf(rang.X)
1431                 if _, isMap := types2.CoreType(xtyp).(*types2.Map); isMap {
1432                         w.rtype(xtyp)
1433                 }
1434                 {
1435                         lhs := syntax.UnpackListExpr(rang.Lhs)
1436                         assign := func(i int, src types2.Type) {
1437                                 if i >= len(lhs) {
1438                                         return
1439                                 }
1440                                 dst := syntax.Unparen(lhs[i])
1441                                 if name, ok := dst.(*syntax.Name); ok && name.Value == "_" {
1442                                         return
1443                                 }
1444
1445                                 var dstType types2.Type
1446                                 if rang.Def {
1447                                         // For `:=` assignments, the LHS names only appear in Defs,
1448                                         // not Types (as used by typeOf).
1449                                         dstType = w.p.info.Defs[dst.(*syntax.Name)].(*types2.Var).Type()
1450                                 } else {
1451                                         dstType = w.p.typeOf(dst)
1452                                 }
1453
1454                                 w.convRTTI(src, dstType)
1455                         }
1456
1457                         keyType, valueType := w.p.rangeTypes(rang.X)
1458                         assign(0, keyType)
1459                         assign(1, valueType)
1460                 }
1461
1462         } else {
1463                 if stmt.Cond != nil && w.p.staticBool(&stmt.Cond) < 0 { // always false
1464                         stmt.Post = nil
1465                         stmt.Body.List = nil
1466                 }
1467
1468                 w.pos(stmt)
1469                 w.stmt(stmt.Init)
1470                 w.optExpr(stmt.Cond)
1471                 w.stmt(stmt.Post)
1472         }
1473
1474         w.blockStmt(stmt.Body)
1475         w.Bool(w.distinctVars(stmt))
1476         w.closeAnotherScope()
1477 }
1478
1479 func (w *writer) distinctVars(stmt *syntax.ForStmt) bool {
1480         lv := base.Debug.LoopVar
1481         v := w.p.info.FileVersions[stmt.Pos().Base()]
1482         is122 := v.Major == 0 && v.Minor == 0 || v.Major == 1 && v.Minor >= 22
1483
1484         // Turning off loopvar for 1.22 is only possible with loopvarhash=qn
1485         //
1486         // Debug.LoopVar values to be preserved for 1.21 compatibility are 1 and 2,
1487         // which are also set (=1) by GOEXPERIMENT=loopvar.  The knobs for turning on
1488         // the new, unshared, loopvar behavior apply to versions less than 1.21 because
1489         // (1) 1.21 also did that and (2) this is believed to be the likely use case;
1490         // anyone checking to see if it affects their code will just run the GOEXPERIMENT
1491         // but will not also update all their go.mod files to 1.21.
1492         //
1493         // -gcflags=-d=loopvar=3 enables logging for 1.22 but does not turn loopvar on for <= 1.21.
1494
1495         return is122 || lv > 0 && lv != 3
1496 }
1497
1498 // rangeTypes returns the types of values produced by ranging over
1499 // expr.
1500 func (pw *pkgWriter) rangeTypes(expr syntax.Expr) (key, value types2.Type) {
1501         typ := pw.typeOf(expr)
1502         switch typ := types2.CoreType(typ).(type) {
1503         case *types2.Pointer: // must be pointer to array
1504                 return types2.Typ[types2.Int], types2.CoreType(typ.Elem()).(*types2.Array).Elem()
1505         case *types2.Array:
1506                 return types2.Typ[types2.Int], typ.Elem()
1507         case *types2.Slice:
1508                 return types2.Typ[types2.Int], typ.Elem()
1509         case *types2.Basic:
1510                 if typ.Info()&types2.IsString != 0 {
1511                         return types2.Typ[types2.Int], runeTypeName.Type()
1512                 }
1513         case *types2.Map:
1514                 return typ.Key(), typ.Elem()
1515         case *types2.Chan:
1516                 return typ.Elem(), nil
1517         }
1518         pw.fatalf(expr, "unexpected range type: %v", typ)
1519         panic("unreachable")
1520 }
1521
1522 func (w *writer) ifStmt(stmt *syntax.IfStmt) {
1523         cond := w.p.staticBool(&stmt.Cond)
1524
1525         w.Sync(pkgbits.SyncIfStmt)
1526         w.openScope(stmt.Pos())
1527         w.pos(stmt)
1528         w.stmt(stmt.Init)
1529         w.expr(stmt.Cond)
1530         w.Int(cond)
1531         if cond >= 0 {
1532                 w.blockStmt(stmt.Then)
1533         } else {
1534                 w.pos(stmt.Then.Rbrace)
1535         }
1536         if cond <= 0 {
1537                 w.stmt(stmt.Else)
1538         }
1539         w.closeAnotherScope()
1540 }
1541
1542 func (w *writer) selectStmt(stmt *syntax.SelectStmt) {
1543         w.Sync(pkgbits.SyncSelectStmt)
1544
1545         w.pos(stmt)
1546         w.Len(len(stmt.Body))
1547         for i, clause := range stmt.Body {
1548                 if i > 0 {
1549                         w.closeScope(clause.Pos())
1550                 }
1551                 w.openScope(clause.Pos())
1552
1553                 w.pos(clause)
1554                 w.stmt(clause.Comm)
1555                 w.stmts(clause.Body)
1556         }
1557         if len(stmt.Body) > 0 {
1558                 w.closeScope(stmt.Rbrace)
1559         }
1560 }
1561
1562 func (w *writer) switchStmt(stmt *syntax.SwitchStmt) {
1563         w.Sync(pkgbits.SyncSwitchStmt)
1564
1565         w.openScope(stmt.Pos())
1566         w.pos(stmt)
1567         w.stmt(stmt.Init)
1568
1569         var iface, tagType types2.Type
1570         if guard, ok := stmt.Tag.(*syntax.TypeSwitchGuard); w.Bool(ok) {
1571                 iface = w.p.typeOf(guard.X)
1572
1573                 w.pos(guard)
1574                 if tag := guard.Lhs; w.Bool(tag != nil) {
1575                         w.pos(tag)
1576
1577                         // Like w.localIdent, but we don't have a types2.Object.
1578                         w.Sync(pkgbits.SyncLocalIdent)
1579                         w.pkg(w.p.curpkg)
1580                         w.String(tag.Value)
1581                 }
1582                 w.expr(guard.X)
1583         } else {
1584                 tag := stmt.Tag
1585
1586                 var tagValue constant.Value
1587                 if tag != nil {
1588                         tv := w.p.typeAndValue(tag)
1589                         tagType = tv.Type
1590                         tagValue = tv.Value
1591                 } else {
1592                         tagType = types2.Typ[types2.Bool]
1593                         tagValue = constant.MakeBool(true)
1594                 }
1595
1596                 if tagValue != nil {
1597                         // If the switch tag has a constant value, look for a case
1598                         // clause that we always branch to.
1599                         func() {
1600                                 var target *syntax.CaseClause
1601                         Outer:
1602                                 for _, clause := range stmt.Body {
1603                                         if clause.Cases == nil {
1604                                                 target = clause
1605                                         }
1606                                         for _, cas := range syntax.UnpackListExpr(clause.Cases) {
1607                                                 tv := w.p.typeAndValue(cas)
1608                                                 if tv.Value == nil {
1609                                                         return // non-constant case; give up
1610                                                 }
1611                                                 if constant.Compare(tagValue, token.EQL, tv.Value) {
1612                                                         target = clause
1613                                                         break Outer
1614                                                 }
1615                                         }
1616                                 }
1617                                 // We've found the target clause, if any.
1618
1619                                 if target != nil {
1620                                         if hasFallthrough(target.Body) {
1621                                                 return // fallthrough is tricky; give up
1622                                         }
1623
1624                                         // Rewrite as single "default" case.
1625                                         target.Cases = nil
1626                                         stmt.Body = []*syntax.CaseClause{target}
1627                                 } else {
1628                                         stmt.Body = nil
1629                                 }
1630
1631                                 // Clear switch tag (i.e., replace with implicit "true").
1632                                 tag = nil
1633                                 stmt.Tag = nil
1634                                 tagType = types2.Typ[types2.Bool]
1635                         }()
1636                 }
1637
1638                 // Walk is going to emit comparisons between the tag value and
1639                 // each case expression, and we want these comparisons to always
1640                 // have the same type. If there are any case values that can't be
1641                 // converted to the tag value's type, then convert everything to
1642                 // `any` instead.
1643         Outer:
1644                 for _, clause := range stmt.Body {
1645                         for _, cas := range syntax.UnpackListExpr(clause.Cases) {
1646                                 if casType := w.p.typeOf(cas); !types2.AssignableTo(casType, tagType) {
1647                                         tagType = types2.NewInterfaceType(nil, nil)
1648                                         break Outer
1649                                 }
1650                         }
1651                 }
1652
1653                 if w.Bool(tag != nil) {
1654                         w.implicitConvExpr(tagType, tag)
1655                 }
1656         }
1657
1658         w.Len(len(stmt.Body))
1659         for i, clause := range stmt.Body {
1660                 if i > 0 {
1661                         w.closeScope(clause.Pos())
1662                 }
1663                 w.openScope(clause.Pos())
1664
1665                 w.pos(clause)
1666
1667                 cases := syntax.UnpackListExpr(clause.Cases)
1668                 if iface != nil {
1669                         w.Len(len(cases))
1670                         for _, cas := range cases {
1671                                 if w.Bool(isNil(w.p, cas)) {
1672                                         continue
1673                                 }
1674                                 w.exprType(iface, cas)
1675                         }
1676                 } else {
1677                         // As if w.exprList(clause.Cases),
1678                         // but with implicit conversions to tagType.
1679
1680                         w.Sync(pkgbits.SyncExprList)
1681                         w.Sync(pkgbits.SyncExprs)
1682                         w.Len(len(cases))
1683                         for _, cas := range cases {
1684                                 w.implicitConvExpr(tagType, cas)
1685                         }
1686                 }
1687
1688                 if obj, ok := w.p.info.Implicits[clause]; ok {
1689                         // TODO(mdempsky): These pos details are quirkish, but also
1690                         // necessary so the variable's position is correct for DWARF
1691                         // scope assignment later. It would probably be better for us to
1692                         // instead just set the variable's DWARF scoping info earlier so
1693                         // we can give it the correct position information.
1694                         pos := clause.Pos()
1695                         if typs := syntax.UnpackListExpr(clause.Cases); len(typs) != 0 {
1696                                 pos = typeExprEndPos(typs[len(typs)-1])
1697                         }
1698                         w.pos(pos)
1699
1700                         obj := obj.(*types2.Var)
1701                         w.typ(obj.Type())
1702                         w.addLocal(obj)
1703                 }
1704
1705                 w.stmts(clause.Body)
1706         }
1707         if len(stmt.Body) > 0 {
1708                 w.closeScope(stmt.Rbrace)
1709         }
1710
1711         w.closeScope(stmt.Rbrace)
1712 }
1713
1714 func (w *writer) label(label *syntax.Name) {
1715         w.Sync(pkgbits.SyncLabel)
1716
1717         // TODO(mdempsky): Replace label strings with dense indices.
1718         w.String(label.Value)
1719 }
1720
1721 func (w *writer) optLabel(label *syntax.Name) {
1722         w.Sync(pkgbits.SyncOptLabel)
1723         if w.Bool(label != nil) {
1724                 w.label(label)
1725         }
1726 }
1727
1728 // @@@ Expressions
1729
1730 // expr writes the given expression into the function body bitstream.
1731 func (w *writer) expr(expr syntax.Expr) {
1732         base.Assertf(expr != nil, "missing expression")
1733
1734         expr = syntax.Unparen(expr) // skip parens; unneeded after typecheck
1735
1736         obj, inst := lookupObj(w.p, expr)
1737         targs := inst.TypeArgs
1738
1739         if tv, ok := w.p.maybeTypeAndValue(expr); ok {
1740                 if tv.IsType() {
1741                         w.p.fatalf(expr, "unexpected type expression %v", syntax.String(expr))
1742                 }
1743
1744                 if tv.Value != nil {
1745                         w.Code(exprConst)
1746                         w.pos(expr)
1747                         typ := idealType(tv)
1748                         assert(typ != nil)
1749                         w.typ(typ)
1750                         w.Value(tv.Value)
1751                         return
1752                 }
1753
1754                 if _, isNil := obj.(*types2.Nil); isNil {
1755                         w.Code(exprNil)
1756                         w.pos(expr)
1757                         w.typ(tv.Type)
1758                         return
1759                 }
1760
1761                 // With shape types (and particular pointer shaping), we may have
1762                 // an expression of type "go.shape.*uint8", but need to reshape it
1763                 // to another shape-identical type to allow use in field
1764                 // selection, indexing, etc.
1765                 if typ := tv.Type; !tv.IsBuiltin() && !isTuple(typ) && !isUntyped(typ) {
1766                         w.Code(exprReshape)
1767                         w.typ(typ)
1768                         // fallthrough
1769                 }
1770         }
1771
1772         if obj != nil {
1773                 if targs.Len() != 0 {
1774                         obj := obj.(*types2.Func)
1775
1776                         w.Code(exprFuncInst)
1777                         w.pos(expr)
1778                         w.funcInst(obj, targs)
1779                         return
1780                 }
1781
1782                 if isGlobal(obj) {
1783                         w.Code(exprGlobal)
1784                         w.obj(obj, nil)
1785                         return
1786                 }
1787
1788                 obj := obj.(*types2.Var)
1789                 assert(!obj.IsField())
1790
1791                 w.Code(exprLocal)
1792                 w.useLocal(expr.Pos(), obj)
1793                 return
1794         }
1795
1796         switch expr := expr.(type) {
1797         default:
1798                 w.p.unexpected("expression", expr)
1799
1800         case *syntax.CompositeLit:
1801                 w.Code(exprCompLit)
1802                 w.compLit(expr)
1803
1804         case *syntax.FuncLit:
1805                 w.Code(exprFuncLit)
1806                 w.funcLit(expr)
1807
1808         case *syntax.SelectorExpr:
1809                 sel, ok := w.p.info.Selections[expr]
1810                 assert(ok)
1811
1812                 switch sel.Kind() {
1813                 default:
1814                         w.p.fatalf(expr, "unexpected selection kind: %v", sel.Kind())
1815
1816                 case types2.FieldVal:
1817                         w.Code(exprFieldVal)
1818                         w.expr(expr.X)
1819                         w.pos(expr)
1820                         w.selector(sel.Obj())
1821
1822                 case types2.MethodVal:
1823                         w.Code(exprMethodVal)
1824                         typ := w.recvExpr(expr, sel)
1825                         w.pos(expr)
1826                         w.methodExpr(expr, typ, sel)
1827
1828                 case types2.MethodExpr:
1829                         w.Code(exprMethodExpr)
1830
1831                         tv := w.p.typeAndValue(expr.X)
1832                         assert(tv.IsType())
1833
1834                         index := sel.Index()
1835                         implicits := index[:len(index)-1]
1836
1837                         typ := tv.Type
1838                         w.typ(typ)
1839
1840                         w.Len(len(implicits))
1841                         for _, ix := range implicits {
1842                                 w.Len(ix)
1843                                 typ = deref2(typ).Underlying().(*types2.Struct).Field(ix).Type()
1844                         }
1845
1846                         recv := sel.Obj().(*types2.Func).Type().(*types2.Signature).Recv().Type()
1847                         if w.Bool(isPtrTo(typ, recv)) { // need deref
1848                                 typ = recv
1849                         } else if w.Bool(isPtrTo(recv, typ)) { // need addr
1850                                 typ = recv
1851                         }
1852
1853                         w.pos(expr)
1854                         w.methodExpr(expr, typ, sel)
1855                 }
1856
1857         case *syntax.IndexExpr:
1858                 _ = w.p.typeOf(expr.Index) // ensure this is an index expression, not an instantiation
1859
1860                 xtyp := w.p.typeOf(expr.X)
1861
1862                 var keyType types2.Type
1863                 if mapType, ok := types2.CoreType(xtyp).(*types2.Map); ok {
1864                         keyType = mapType.Key()
1865                 }
1866
1867                 w.Code(exprIndex)
1868                 w.expr(expr.X)
1869                 w.pos(expr)
1870                 w.implicitConvExpr(keyType, expr.Index)
1871                 if keyType != nil {
1872                         w.rtype(xtyp)
1873                 }
1874
1875         case *syntax.SliceExpr:
1876                 w.Code(exprSlice)
1877                 w.expr(expr.X)
1878                 w.pos(expr)
1879                 for _, n := range &expr.Index {
1880                         w.optExpr(n)
1881                 }
1882
1883         case *syntax.AssertExpr:
1884                 iface := w.p.typeOf(expr.X)
1885
1886                 w.Code(exprAssert)
1887                 w.expr(expr.X)
1888                 w.pos(expr)
1889                 w.exprType(iface, expr.Type)
1890                 w.rtype(iface)
1891
1892         case *syntax.Operation:
1893                 if expr.Y == nil {
1894                         w.Code(exprUnaryOp)
1895                         w.op(unOps[expr.Op])
1896                         w.pos(expr)
1897                         w.expr(expr.X)
1898                         break
1899                 }
1900
1901                 var commonType types2.Type
1902                 switch expr.Op {
1903                 case syntax.Shl, syntax.Shr:
1904                         // ok: operands are allowed to have different types
1905                 default:
1906                         xtyp := w.p.typeOf(expr.X)
1907                         ytyp := w.p.typeOf(expr.Y)
1908                         switch {
1909                         case types2.AssignableTo(xtyp, ytyp):
1910                                 commonType = ytyp
1911                         case types2.AssignableTo(ytyp, xtyp):
1912                                 commonType = xtyp
1913                         default:
1914                                 w.p.fatalf(expr, "failed to find common type between %v and %v", xtyp, ytyp)
1915                         }
1916                 }
1917
1918                 w.Code(exprBinaryOp)
1919                 w.op(binOps[expr.Op])
1920                 w.implicitConvExpr(commonType, expr.X)
1921                 w.pos(expr)
1922                 w.implicitConvExpr(commonType, expr.Y)
1923
1924         case *syntax.CallExpr:
1925                 tv := w.p.typeAndValue(expr.Fun)
1926                 if tv.IsType() {
1927                         assert(len(expr.ArgList) == 1)
1928                         assert(!expr.HasDots)
1929                         w.convertExpr(tv.Type, expr.ArgList[0], false)
1930                         break
1931                 }
1932
1933                 var rtype types2.Type
1934                 if tv.IsBuiltin() {
1935                         switch obj, _ := lookupObj(w.p, expr.Fun); obj.Name() {
1936                         case "make":
1937                                 assert(len(expr.ArgList) >= 1)
1938                                 assert(!expr.HasDots)
1939
1940                                 w.Code(exprMake)
1941                                 w.pos(expr)
1942                                 w.exprType(nil, expr.ArgList[0])
1943                                 w.exprs(expr.ArgList[1:])
1944
1945                                 typ := w.p.typeOf(expr)
1946                                 switch coreType := types2.CoreType(typ).(type) {
1947                                 default:
1948                                         w.p.fatalf(expr, "unexpected core type: %v", coreType)
1949                                 case *types2.Chan:
1950                                         w.rtype(typ)
1951                                 case *types2.Map:
1952                                         w.rtype(typ)
1953                                 case *types2.Slice:
1954                                         w.rtype(sliceElem(typ))
1955                                 }
1956
1957                                 return
1958
1959                         case "new":
1960                                 assert(len(expr.ArgList) == 1)
1961                                 assert(!expr.HasDots)
1962
1963                                 w.Code(exprNew)
1964                                 w.pos(expr)
1965                                 w.exprType(nil, expr.ArgList[0])
1966                                 return
1967
1968                         case "append":
1969                                 rtype = sliceElem(w.p.typeOf(expr))
1970                         case "copy":
1971                                 typ := w.p.typeOf(expr.ArgList[0])
1972                                 if tuple, ok := typ.(*types2.Tuple); ok { // "copy(g())"
1973                                         typ = tuple.At(0).Type()
1974                                 }
1975                                 rtype = sliceElem(typ)
1976                         case "delete":
1977                                 typ := w.p.typeOf(expr.ArgList[0])
1978                                 if tuple, ok := typ.(*types2.Tuple); ok { // "delete(g())"
1979                                         typ = tuple.At(0).Type()
1980                                 }
1981                                 rtype = typ
1982                         case "Slice":
1983                                 rtype = sliceElem(w.p.typeOf(expr))
1984                         }
1985                 }
1986
1987                 writeFunExpr := func() {
1988                         fun := syntax.Unparen(expr.Fun)
1989
1990                         if selector, ok := fun.(*syntax.SelectorExpr); ok {
1991                                 if sel, ok := w.p.info.Selections[selector]; ok && sel.Kind() == types2.MethodVal {
1992                                         w.Bool(true) // method call
1993                                         typ := w.recvExpr(selector, sel)
1994                                         w.methodExpr(selector, typ, sel)
1995                                         return
1996                                 }
1997                         }
1998
1999                         w.Bool(false) // not a method call (i.e., normal function call)
2000
2001                         if obj, inst := lookupObj(w.p, fun); w.Bool(obj != nil && inst.TypeArgs.Len() != 0) {
2002                                 obj := obj.(*types2.Func)
2003
2004                                 w.pos(fun)
2005                                 w.funcInst(obj, inst.TypeArgs)
2006                                 return
2007                         }
2008
2009                         w.expr(fun)
2010                 }
2011
2012                 sigType := types2.CoreType(tv.Type).(*types2.Signature)
2013                 paramTypes := sigType.Params()
2014
2015                 w.Code(exprCall)
2016                 writeFunExpr()
2017                 w.pos(expr)
2018
2019                 paramType := func(i int) types2.Type {
2020                         if sigType.Variadic() && !expr.HasDots && i >= paramTypes.Len()-1 {
2021                                 return paramTypes.At(paramTypes.Len() - 1).Type().(*types2.Slice).Elem()
2022                         }
2023                         return paramTypes.At(i).Type()
2024                 }
2025
2026                 w.multiExpr(expr, paramType, expr.ArgList)
2027                 w.Bool(expr.HasDots)
2028                 if rtype != nil {
2029                         w.rtype(rtype)
2030                 }
2031         }
2032 }
2033
2034 func sliceElem(typ types2.Type) types2.Type {
2035         return types2.CoreType(typ).(*types2.Slice).Elem()
2036 }
2037
2038 func (w *writer) optExpr(expr syntax.Expr) {
2039         if w.Bool(expr != nil) {
2040                 w.expr(expr)
2041         }
2042 }
2043
2044 // recvExpr writes out expr.X, but handles any implicit addressing,
2045 // dereferencing, and field selections appropriate for the method
2046 // selection.
2047 func (w *writer) recvExpr(expr *syntax.SelectorExpr, sel *types2.Selection) types2.Type {
2048         index := sel.Index()
2049         implicits := index[:len(index)-1]
2050
2051         w.Code(exprRecv)
2052         w.expr(expr.X)
2053         w.pos(expr)
2054         w.Len(len(implicits))
2055
2056         typ := w.p.typeOf(expr.X)
2057         for _, ix := range implicits {
2058                 typ = deref2(typ).Underlying().(*types2.Struct).Field(ix).Type()
2059                 w.Len(ix)
2060         }
2061
2062         recv := sel.Obj().(*types2.Func).Type().(*types2.Signature).Recv().Type()
2063         if w.Bool(isPtrTo(typ, recv)) { // needs deref
2064                 typ = recv
2065         } else if w.Bool(isPtrTo(recv, typ)) { // needs addr
2066                 typ = recv
2067         }
2068
2069         return typ
2070 }
2071
2072 // funcInst writes a reference to an instantiated function.
2073 func (w *writer) funcInst(obj *types2.Func, targs *types2.TypeList) {
2074         info := w.p.objInstIdx(obj, targs, w.dict)
2075
2076         // Type arguments list contains derived types; we can emit a static
2077         // call to the shaped function, but need to dynamically compute the
2078         // runtime dictionary pointer.
2079         if w.Bool(info.anyDerived()) {
2080                 w.Len(w.dict.subdictIdx(info))
2081                 return
2082         }
2083
2084         // Type arguments list is statically known; we can emit a static
2085         // call with a statically reference to the respective runtime
2086         // dictionary.
2087         w.objInfo(info)
2088 }
2089
2090 // methodExpr writes out a reference to the method selected by
2091 // expr. sel should be the corresponding types2.Selection, and recv
2092 // the type produced after any implicit addressing, dereferencing, and
2093 // field selection. (Note: recv might differ from sel.Obj()'s receiver
2094 // parameter in the case of interface types, and is needed for
2095 // handling type parameter methods.)
2096 func (w *writer) methodExpr(expr *syntax.SelectorExpr, recv types2.Type, sel *types2.Selection) {
2097         fun := sel.Obj().(*types2.Func)
2098         sig := fun.Type().(*types2.Signature)
2099
2100         w.typ(recv)
2101         w.typ(sig)
2102         w.pos(expr)
2103         w.selector(fun)
2104
2105         // Method on a type parameter. These require an indirect call
2106         // through the current function's runtime dictionary.
2107         if typeParam, ok := recv.(*types2.TypeParam); w.Bool(ok) {
2108                 typeParamIdx := w.dict.typeParamIndex(typeParam)
2109                 methodInfo := w.p.selectorIdx(fun)
2110
2111                 w.Len(w.dict.typeParamMethodExprIdx(typeParamIdx, methodInfo))
2112                 return
2113         }
2114
2115         if isInterface(recv) != isInterface(sig.Recv().Type()) {
2116                 w.p.fatalf(expr, "isInterface inconsistency: %v and %v", recv, sig.Recv().Type())
2117         }
2118
2119         if !isInterface(recv) {
2120                 if named, ok := deref2(recv).(*types2.Named); ok {
2121                         obj, targs := splitNamed(named)
2122                         info := w.p.objInstIdx(obj, targs, w.dict)
2123
2124                         // Method on a derived receiver type. These can be handled by a
2125                         // static call to the shaped method, but require dynamically
2126                         // looking up the appropriate dictionary argument in the current
2127                         // function's runtime dictionary.
2128                         if w.p.hasImplicitTypeParams(obj) || info.anyDerived() {
2129                                 w.Bool(true) // dynamic subdictionary
2130                                 w.Len(w.dict.subdictIdx(info))
2131                                 return
2132                         }
2133
2134                         // Method on a fully known receiver type. These can be handled
2135                         // by a static call to the shaped method, and with a static
2136                         // reference to the receiver type's dictionary.
2137                         if targs.Len() != 0 {
2138                                 w.Bool(false) // no dynamic subdictionary
2139                                 w.Bool(true)  // static dictionary
2140                                 w.objInfo(info)
2141                                 return
2142                         }
2143                 }
2144         }
2145
2146         w.Bool(false) // no dynamic subdictionary
2147         w.Bool(false) // no static dictionary
2148 }
2149
2150 // multiExpr writes a sequence of expressions, where the i'th value is
2151 // implicitly converted to dstType(i). It also handles when exprs is a
2152 // single, multi-valued expression (e.g., the multi-valued argument in
2153 // an f(g()) call, or the RHS operand in a comma-ok assignment).
2154 func (w *writer) multiExpr(pos poser, dstType func(int) types2.Type, exprs []syntax.Expr) {
2155         w.Sync(pkgbits.SyncMultiExpr)
2156
2157         if len(exprs) == 1 {
2158                 expr := exprs[0]
2159                 if tuple, ok := w.p.typeOf(expr).(*types2.Tuple); ok {
2160                         assert(tuple.Len() > 1)
2161                         w.Bool(true) // N:1 assignment
2162                         w.pos(pos)
2163                         w.expr(expr)
2164
2165                         w.Len(tuple.Len())
2166                         for i := 0; i < tuple.Len(); i++ {
2167                                 src := tuple.At(i).Type()
2168                                 // TODO(mdempsky): Investigate not writing src here. I think
2169                                 // the reader should be able to infer it from expr anyway.
2170                                 w.typ(src)
2171                                 if dst := dstType(i); w.Bool(dst != nil && !types2.Identical(src, dst)) {
2172                                         if src == nil || dst == nil {
2173                                                 w.p.fatalf(pos, "src is %v, dst is %v", src, dst)
2174                                         }
2175                                         if !types2.AssignableTo(src, dst) {
2176                                                 w.p.fatalf(pos, "%v is not assignable to %v", src, dst)
2177                                         }
2178                                         w.typ(dst)
2179                                         w.convRTTI(src, dst)
2180                                 }
2181                         }
2182                         return
2183                 }
2184         }
2185
2186         w.Bool(false) // N:N assignment
2187         w.Len(len(exprs))
2188         for i, expr := range exprs {
2189                 w.implicitConvExpr(dstType(i), expr)
2190         }
2191 }
2192
2193 // implicitConvExpr is like expr, but if dst is non-nil and different
2194 // from expr's type, then an implicit conversion operation is inserted
2195 // at expr's position.
2196 func (w *writer) implicitConvExpr(dst types2.Type, expr syntax.Expr) {
2197         w.convertExpr(dst, expr, true)
2198 }
2199
2200 func (w *writer) convertExpr(dst types2.Type, expr syntax.Expr, implicit bool) {
2201         src := w.p.typeOf(expr)
2202
2203         // Omit implicit no-op conversions.
2204         identical := dst == nil || types2.Identical(src, dst)
2205         if implicit && identical {
2206                 w.expr(expr)
2207                 return
2208         }
2209
2210         if implicit && !types2.AssignableTo(src, dst) {
2211                 w.p.fatalf(expr, "%v is not assignable to %v", src, dst)
2212         }
2213
2214         w.Code(exprConvert)
2215         w.Bool(implicit)
2216         w.typ(dst)
2217         w.pos(expr)
2218         w.convRTTI(src, dst)
2219         w.Bool(isTypeParam(dst))
2220         w.Bool(identical)
2221         w.expr(expr)
2222 }
2223
2224 func (w *writer) compLit(lit *syntax.CompositeLit) {
2225         typ := w.p.typeOf(lit)
2226
2227         w.Sync(pkgbits.SyncCompLit)
2228         w.pos(lit)
2229         w.typ(typ)
2230
2231         if ptr, ok := types2.CoreType(typ).(*types2.Pointer); ok {
2232                 typ = ptr.Elem()
2233         }
2234         var keyType, elemType types2.Type
2235         var structType *types2.Struct
2236         switch typ0 := typ; typ := types2.CoreType(typ).(type) {
2237         default:
2238                 w.p.fatalf(lit, "unexpected composite literal type: %v", typ)
2239         case *types2.Array:
2240                 elemType = typ.Elem()
2241         case *types2.Map:
2242                 w.rtype(typ0)
2243                 keyType, elemType = typ.Key(), typ.Elem()
2244         case *types2.Slice:
2245                 elemType = typ.Elem()
2246         case *types2.Struct:
2247                 structType = typ
2248         }
2249
2250         w.Len(len(lit.ElemList))
2251         for i, elem := range lit.ElemList {
2252                 elemType := elemType
2253                 if structType != nil {
2254                         if kv, ok := elem.(*syntax.KeyValueExpr); ok {
2255                                 // use position of expr.Key rather than of elem (which has position of ':')
2256                                 w.pos(kv.Key)
2257                                 i = fieldIndex(w.p.info, structType, kv.Key.(*syntax.Name))
2258                                 elem = kv.Value
2259                         } else {
2260                                 w.pos(elem)
2261                         }
2262                         elemType = structType.Field(i).Type()
2263                         w.Len(i)
2264                 } else {
2265                         if kv, ok := elem.(*syntax.KeyValueExpr); w.Bool(ok) {
2266                                 // use position of expr.Key rather than of elem (which has position of ':')
2267                                 w.pos(kv.Key)
2268                                 w.implicitConvExpr(keyType, kv.Key)
2269                                 elem = kv.Value
2270                         }
2271                 }
2272                 w.pos(elem)
2273                 w.implicitConvExpr(elemType, elem)
2274         }
2275 }
2276
2277 func (w *writer) funcLit(expr *syntax.FuncLit) {
2278         sig := w.p.typeOf(expr).(*types2.Signature)
2279
2280         body, closureVars := w.p.bodyIdx(sig, expr.Body, w.dict)
2281
2282         w.Sync(pkgbits.SyncFuncLit)
2283         w.pos(expr)
2284         w.signature(sig)
2285
2286         w.Len(len(closureVars))
2287         for _, cv := range closureVars {
2288                 w.pos(cv.pos)
2289                 w.useLocal(cv.pos, cv.var_)
2290         }
2291
2292         w.Reloc(pkgbits.RelocBody, body)
2293 }
2294
2295 type posVar struct {
2296         pos  syntax.Pos
2297         var_ *types2.Var
2298 }
2299
2300 func (w *writer) exprList(expr syntax.Expr) {
2301         w.Sync(pkgbits.SyncExprList)
2302         w.exprs(syntax.UnpackListExpr(expr))
2303 }
2304
2305 func (w *writer) exprs(exprs []syntax.Expr) {
2306         w.Sync(pkgbits.SyncExprs)
2307         w.Len(len(exprs))
2308         for _, expr := range exprs {
2309                 w.expr(expr)
2310         }
2311 }
2312
2313 // rtype writes information so that the reader can construct an
2314 // expression of type *runtime._type representing typ.
2315 func (w *writer) rtype(typ types2.Type) {
2316         typ = types2.Default(typ)
2317
2318         info := w.p.typIdx(typ, w.dict)
2319         w.rtypeInfo(info)
2320 }
2321
2322 func (w *writer) rtypeInfo(info typeInfo) {
2323         w.Sync(pkgbits.SyncRType)
2324
2325         if w.Bool(info.derived) {
2326                 w.Len(w.dict.rtypeIdx(info))
2327         } else {
2328                 w.typInfo(info)
2329         }
2330 }
2331
2332 // varDictIndex writes out information for populating DictIndex for
2333 // the ir.Name that will represent obj.
2334 func (w *writer) varDictIndex(obj *types2.Var) {
2335         info := w.p.typIdx(obj.Type(), w.dict)
2336         if w.Bool(info.derived) {
2337                 w.Len(w.dict.rtypeIdx(info))
2338         }
2339 }
2340
2341 func isUntyped(typ types2.Type) bool {
2342         basic, ok := typ.(*types2.Basic)
2343         return ok && basic.Info()&types2.IsUntyped != 0
2344 }
2345
2346 func isTuple(typ types2.Type) bool {
2347         _, ok := typ.(*types2.Tuple)
2348         return ok
2349 }
2350
2351 func (w *writer) itab(typ, iface types2.Type) {
2352         typ = types2.Default(typ)
2353         iface = types2.Default(iface)
2354
2355         typInfo := w.p.typIdx(typ, w.dict)
2356         ifaceInfo := w.p.typIdx(iface, w.dict)
2357
2358         w.rtypeInfo(typInfo)
2359         w.rtypeInfo(ifaceInfo)
2360         if w.Bool(typInfo.derived || ifaceInfo.derived) {
2361                 w.Len(w.dict.itabIdx(typInfo, ifaceInfo))
2362         }
2363 }
2364
2365 // convRTTI writes information so that the reader can construct
2366 // expressions for converting from src to dst.
2367 func (w *writer) convRTTI(src, dst types2.Type) {
2368         w.Sync(pkgbits.SyncConvRTTI)
2369         w.itab(src, dst)
2370 }
2371
2372 func (w *writer) exprType(iface types2.Type, typ syntax.Expr) {
2373         base.Assertf(iface == nil || isInterface(iface), "%v must be nil or an interface type", iface)
2374
2375         tv := w.p.typeAndValue(typ)
2376         assert(tv.IsType())
2377
2378         w.Sync(pkgbits.SyncExprType)
2379         w.pos(typ)
2380
2381         if w.Bool(iface != nil && !iface.Underlying().(*types2.Interface).Empty()) {
2382                 w.itab(tv.Type, iface)
2383         } else {
2384                 w.rtype(tv.Type)
2385
2386                 info := w.p.typIdx(tv.Type, w.dict)
2387                 w.Bool(info.derived)
2388         }
2389 }
2390
2391 // isInterface reports whether typ is known to be an interface type.
2392 // If typ is a type parameter, then isInterface reports an internal
2393 // compiler error instead.
2394 func isInterface(typ types2.Type) bool {
2395         if _, ok := typ.(*types2.TypeParam); ok {
2396                 // typ is a type parameter and may be instantiated as either a
2397                 // concrete or interface type, so the writer can't depend on
2398                 // knowing this.
2399                 base.Fatalf("%v is a type parameter", typ)
2400         }
2401
2402         _, ok := typ.Underlying().(*types2.Interface)
2403         return ok
2404 }
2405
2406 // op writes an Op into the bitstream.
2407 func (w *writer) op(op ir.Op) {
2408         // TODO(mdempsky): Remove in favor of explicit codes? Would make
2409         // export data more stable against internal refactorings, but low
2410         // priority at the moment.
2411         assert(op != 0)
2412         w.Sync(pkgbits.SyncOp)
2413         w.Len(int(op))
2414 }
2415
2416 // @@@ Package initialization
2417
2418 // Caution: This code is still clumsy, because toolstash -cmp is
2419 // particularly sensitive to it.
2420
2421 type typeDeclGen struct {
2422         *syntax.TypeDecl
2423         gen int
2424
2425         // Implicit type parameters in scope at this type declaration.
2426         implicits []*types2.TypeName
2427 }
2428
2429 type fileImports struct {
2430         importedEmbed, importedUnsafe bool
2431 }
2432
2433 // declCollector is a visitor type that collects compiler-needed
2434 // information about declarations that types2 doesn't track.
2435 //
2436 // Notably, it maps declared types and functions back to their
2437 // declaration statement, keeps track of implicit type parameters, and
2438 // assigns unique type "generation" numbers to local defined types.
2439 type declCollector struct {
2440         pw         *pkgWriter
2441         typegen    *int
2442         file       *fileImports
2443         withinFunc bool
2444         implicits  []*types2.TypeName
2445 }
2446
2447 func (c *declCollector) withTParams(obj types2.Object) *declCollector {
2448         tparams := objTypeParams(obj)
2449         n := tparams.Len()
2450         if n == 0 {
2451                 return c
2452         }
2453
2454         copy := *c
2455         copy.implicits = copy.implicits[:len(copy.implicits):len(copy.implicits)]
2456         for i := 0; i < n; i++ {
2457                 copy.implicits = append(copy.implicits, tparams.At(i).Obj())
2458         }
2459         return &copy
2460 }
2461
2462 func (c *declCollector) Visit(n syntax.Node) syntax.Visitor {
2463         pw := c.pw
2464
2465         switch n := n.(type) {
2466         case *syntax.File:
2467                 pw.checkPragmas(n.Pragma, ir.GoBuildPragma, false)
2468
2469         case *syntax.ImportDecl:
2470                 pw.checkPragmas(n.Pragma, 0, false)
2471
2472                 switch pkgNameOf(pw.info, n).Imported().Path() {
2473                 case "embed":
2474                         c.file.importedEmbed = true
2475                 case "unsafe":
2476                         c.file.importedUnsafe = true
2477                 }
2478
2479         case *syntax.ConstDecl:
2480                 pw.checkPragmas(n.Pragma, 0, false)
2481
2482         case *syntax.FuncDecl:
2483                 pw.checkPragmas(n.Pragma, funcPragmas, false)
2484
2485                 obj := pw.info.Defs[n.Name].(*types2.Func)
2486                 pw.funDecls[obj] = n
2487
2488                 return c.withTParams(obj)
2489
2490         case *syntax.TypeDecl:
2491                 obj := pw.info.Defs[n.Name].(*types2.TypeName)
2492                 d := typeDeclGen{TypeDecl: n, implicits: c.implicits}
2493
2494                 if n.Alias {
2495                         pw.checkPragmas(n.Pragma, 0, false)
2496                 } else {
2497                         pw.checkPragmas(n.Pragma, 0, false)
2498
2499                         // Assign a unique ID to function-scoped defined types.
2500                         if c.withinFunc {
2501                                 *c.typegen++
2502                                 d.gen = *c.typegen
2503                         }
2504                 }
2505
2506                 pw.typDecls[obj] = d
2507
2508                 // TODO(mdempsky): Omit? Not strictly necessary; only matters for
2509                 // type declarations within function literals within parameterized
2510                 // type declarations, but types2 the function literals will be
2511                 // constant folded away.
2512                 return c.withTParams(obj)
2513
2514         case *syntax.VarDecl:
2515                 pw.checkPragmas(n.Pragma, 0, true)
2516
2517                 if p, ok := n.Pragma.(*pragmas); ok && len(p.Embeds) > 0 {
2518                         if err := checkEmbed(n, c.file.importedEmbed, c.withinFunc); err != nil {
2519                                 pw.errorf(p.Embeds[0].Pos, "%s", err)
2520                         }
2521                 }
2522
2523         case *syntax.BlockStmt:
2524                 if !c.withinFunc {
2525                         copy := *c
2526                         copy.withinFunc = true
2527                         return &copy
2528                 }
2529         }
2530
2531         return c
2532 }
2533
2534 func (pw *pkgWriter) collectDecls(noders []*noder) {
2535         var typegen int
2536         for _, p := range noders {
2537                 var file fileImports
2538
2539                 syntax.Walk(p.file, &declCollector{
2540                         pw:      pw,
2541                         typegen: &typegen,
2542                         file:    &file,
2543                 })
2544
2545                 pw.cgoPragmas = append(pw.cgoPragmas, p.pragcgobuf...)
2546
2547                 for _, l := range p.linknames {
2548                         if !file.importedUnsafe {
2549                                 pw.errorf(l.pos, "//go:linkname only allowed in Go files that import \"unsafe\"")
2550                                 continue
2551                         }
2552
2553                         switch obj := pw.curpkg.Scope().Lookup(l.local).(type) {
2554                         case *types2.Func, *types2.Var:
2555                                 if _, ok := pw.linknames[obj]; !ok {
2556                                         pw.linknames[obj] = l.remote
2557                                 } else {
2558                                         pw.errorf(l.pos, "duplicate //go:linkname for %s", l.local)
2559                                 }
2560
2561                         default:
2562                                 if types.AllowsGoVersion(1, 18) {
2563                                         pw.errorf(l.pos, "//go:linkname must refer to declared function or variable")
2564                                 }
2565                         }
2566                 }
2567         }
2568 }
2569
2570 func (pw *pkgWriter) checkPragmas(p syntax.Pragma, allowed ir.PragmaFlag, embedOK bool) {
2571         if p == nil {
2572                 return
2573         }
2574         pragma := p.(*pragmas)
2575
2576         for _, pos := range pragma.Pos {
2577                 if pos.Flag&^allowed != 0 {
2578                         pw.errorf(pos.Pos, "misplaced compiler directive")
2579                 }
2580         }
2581
2582         if !embedOK {
2583                 for _, e := range pragma.Embeds {
2584                         pw.errorf(e.Pos, "misplaced go:embed directive")
2585                 }
2586         }
2587 }
2588
2589 func (w *writer) pkgInit(noders []*noder) {
2590         w.Len(len(w.p.cgoPragmas))
2591         for _, cgoPragma := range w.p.cgoPragmas {
2592                 w.Strings(cgoPragma)
2593         }
2594
2595         w.pkgInitOrder()
2596
2597         w.Sync(pkgbits.SyncDecls)
2598         for _, p := range noders {
2599                 for _, decl := range p.file.DeclList {
2600                         w.pkgDecl(decl)
2601                 }
2602         }
2603         w.Code(declEnd)
2604
2605         w.Sync(pkgbits.SyncEOF)
2606 }
2607
2608 func (w *writer) pkgInitOrder() {
2609         // TODO(mdempsky): Write as a function body instead?
2610         w.Len(len(w.p.info.InitOrder))
2611         for _, init := range w.p.info.InitOrder {
2612                 w.Len(len(init.Lhs))
2613                 for _, v := range init.Lhs {
2614                         w.obj(v, nil)
2615                 }
2616                 w.expr(init.Rhs)
2617         }
2618 }
2619
2620 func (w *writer) pkgDecl(decl syntax.Decl) {
2621         switch decl := decl.(type) {
2622         default:
2623                 w.p.unexpected("declaration", decl)
2624
2625         case *syntax.ImportDecl:
2626
2627         case *syntax.ConstDecl:
2628                 w.Code(declOther)
2629                 w.pkgObjs(decl.NameList...)
2630
2631         case *syntax.FuncDecl:
2632                 if decl.Name.Value == "_" {
2633                         break // skip blank functions
2634                 }
2635
2636                 obj := w.p.info.Defs[decl.Name].(*types2.Func)
2637                 sig := obj.Type().(*types2.Signature)
2638
2639                 if sig.RecvTypeParams() != nil || sig.TypeParams() != nil {
2640                         break // skip generic functions
2641                 }
2642
2643                 if recv := sig.Recv(); recv != nil {
2644                         w.Code(declMethod)
2645                         w.typ(recvBase(recv))
2646                         w.selector(obj)
2647                         break
2648                 }
2649
2650                 w.Code(declFunc)
2651                 w.pkgObjs(decl.Name)
2652
2653         case *syntax.TypeDecl:
2654                 if len(decl.TParamList) != 0 {
2655                         break // skip generic type decls
2656                 }
2657
2658                 if decl.Name.Value == "_" {
2659                         break // skip blank type decls
2660                 }
2661
2662                 name := w.p.info.Defs[decl.Name].(*types2.TypeName)
2663                 // Skip type declarations for interfaces that are only usable as
2664                 // type parameter bounds.
2665                 if iface, ok := name.Type().Underlying().(*types2.Interface); ok && !iface.IsMethodSet() {
2666                         break
2667                 }
2668
2669                 w.Code(declOther)
2670                 w.pkgObjs(decl.Name)
2671
2672         case *syntax.VarDecl:
2673                 w.Code(declVar)
2674                 w.pkgObjs(decl.NameList...)
2675
2676                 var embeds []pragmaEmbed
2677                 if p, ok := decl.Pragma.(*pragmas); ok {
2678                         embeds = p.Embeds
2679                 }
2680                 w.Len(len(embeds))
2681                 for _, embed := range embeds {
2682                         w.pos(embed.Pos)
2683                         w.Strings(embed.Patterns)
2684                 }
2685         }
2686 }
2687
2688 func (w *writer) pkgObjs(names ...*syntax.Name) {
2689         w.Sync(pkgbits.SyncDeclNames)
2690         w.Len(len(names))
2691
2692         for _, name := range names {
2693                 obj, ok := w.p.info.Defs[name]
2694                 assert(ok)
2695
2696                 w.Sync(pkgbits.SyncDeclName)
2697                 w.obj(obj, nil)
2698         }
2699 }
2700
2701 // @@@ Helpers
2702
2703 // staticBool analyzes a boolean expression and reports whether it's
2704 // always true (positive result), always false (negative result), or
2705 // unknown (zero).
2706 //
2707 // It also simplifies the expression while preserving semantics, if
2708 // possible.
2709 func (pw *pkgWriter) staticBool(ep *syntax.Expr) int {
2710         if val := pw.typeAndValue(*ep).Value; val != nil {
2711                 if constant.BoolVal(val) {
2712                         return +1
2713                 } else {
2714                         return -1
2715                 }
2716         }
2717
2718         if e, ok := (*ep).(*syntax.Operation); ok {
2719                 switch e.Op {
2720                 case syntax.Not:
2721                         return pw.staticBool(&e.X)
2722
2723                 case syntax.AndAnd:
2724                         x := pw.staticBool(&e.X)
2725                         if x < 0 {
2726                                 *ep = e.X
2727                                 return x
2728                         }
2729
2730                         y := pw.staticBool(&e.Y)
2731                         if x > 0 || y < 0 {
2732                                 if pw.typeAndValue(e.X).Value != nil {
2733                                         *ep = e.Y
2734                                 }
2735                                 return y
2736                         }
2737
2738                 case syntax.OrOr:
2739                         x := pw.staticBool(&e.X)
2740                         if x > 0 {
2741                                 *ep = e.X
2742                                 return x
2743                         }
2744
2745                         y := pw.staticBool(&e.Y)
2746                         if x < 0 || y > 0 {
2747                                 if pw.typeAndValue(e.X).Value != nil {
2748                                         *ep = e.Y
2749                                 }
2750                                 return y
2751                         }
2752                 }
2753         }
2754
2755         return 0
2756 }
2757
2758 // hasImplicitTypeParams reports whether obj is a defined type with
2759 // implicit type parameters (e.g., declared within a generic function
2760 // or method).
2761 func (pw *pkgWriter) hasImplicitTypeParams(obj *types2.TypeName) bool {
2762         if obj.Pkg() == pw.curpkg {
2763                 decl, ok := pw.typDecls[obj]
2764                 assert(ok)
2765                 if len(decl.implicits) != 0 {
2766                         return true
2767                 }
2768         }
2769         return false
2770 }
2771
2772 // isDefinedType reports whether obj is a defined type.
2773 func isDefinedType(obj types2.Object) bool {
2774         if obj, ok := obj.(*types2.TypeName); ok {
2775                 return !obj.IsAlias()
2776         }
2777         return false
2778 }
2779
2780 // isGlobal reports whether obj was declared at package scope.
2781 //
2782 // Caveat: blank objects are not declared.
2783 func isGlobal(obj types2.Object) bool {
2784         return obj.Parent() == obj.Pkg().Scope()
2785 }
2786
2787 // lookupObj returns the object that expr refers to, if any. If expr
2788 // is an explicit instantiation of a generic object, then the instance
2789 // object is returned as well.
2790 func lookupObj(p *pkgWriter, expr syntax.Expr) (obj types2.Object, inst types2.Instance) {
2791         if index, ok := expr.(*syntax.IndexExpr); ok {
2792                 args := syntax.UnpackListExpr(index.Index)
2793                 if len(args) == 1 {
2794                         tv := p.typeAndValue(args[0])
2795                         if tv.IsValue() {
2796                                 return // normal index expression
2797                         }
2798                 }
2799
2800                 expr = index.X
2801         }
2802
2803         // Strip package qualifier, if present.
2804         if sel, ok := expr.(*syntax.SelectorExpr); ok {
2805                 if !isPkgQual(p.info, sel) {
2806                         return // normal selector expression
2807                 }
2808                 expr = sel.Sel
2809         }
2810
2811         if name, ok := expr.(*syntax.Name); ok {
2812                 obj = p.info.Uses[name]
2813                 inst = p.info.Instances[name]
2814         }
2815         return
2816 }
2817
2818 // isPkgQual reports whether the given selector expression is a
2819 // package-qualified identifier.
2820 func isPkgQual(info *types2.Info, sel *syntax.SelectorExpr) bool {
2821         if name, ok := sel.X.(*syntax.Name); ok {
2822                 _, isPkgName := info.Uses[name].(*types2.PkgName)
2823                 return isPkgName
2824         }
2825         return false
2826 }
2827
2828 // isNil reports whether expr is a (possibly parenthesized) reference
2829 // to the predeclared nil value.
2830 func isNil(p *pkgWriter, expr syntax.Expr) bool {
2831         tv := p.typeAndValue(expr)
2832         return tv.IsNil()
2833 }
2834
2835 // isBuiltin reports whether expr is a (possibly parenthesized)
2836 // referenced to the specified built-in function.
2837 func (pw *pkgWriter) isBuiltin(expr syntax.Expr, builtin string) bool {
2838         if name, ok := syntax.Unparen(expr).(*syntax.Name); ok && name.Value == builtin {
2839                 return pw.typeAndValue(name).IsBuiltin()
2840         }
2841         return false
2842 }
2843
2844 // recvBase returns the base type for the given receiver parameter.
2845 func recvBase(recv *types2.Var) *types2.Named {
2846         typ := recv.Type()
2847         if ptr, ok := typ.(*types2.Pointer); ok {
2848                 typ = ptr.Elem()
2849         }
2850         return typ.(*types2.Named)
2851 }
2852
2853 // namesAsExpr returns a list of names as a syntax.Expr.
2854 func namesAsExpr(names []*syntax.Name) syntax.Expr {
2855         if len(names) == 1 {
2856                 return names[0]
2857         }
2858
2859         exprs := make([]syntax.Expr, len(names))
2860         for i, name := range names {
2861                 exprs[i] = name
2862         }
2863         return &syntax.ListExpr{ElemList: exprs}
2864 }
2865
2866 // fieldIndex returns the index of the struct field named by key.
2867 func fieldIndex(info *types2.Info, str *types2.Struct, key *syntax.Name) int {
2868         field := info.Uses[key].(*types2.Var)
2869
2870         for i := 0; i < str.NumFields(); i++ {
2871                 if str.Field(i) == field {
2872                         return i
2873                 }
2874         }
2875
2876         panic(fmt.Sprintf("%s: %v is not a field of %v", key.Pos(), field, str))
2877 }
2878
2879 // objTypeParams returns the type parameters on the given object.
2880 func objTypeParams(obj types2.Object) *types2.TypeParamList {
2881         switch obj := obj.(type) {
2882         case *types2.Func:
2883                 sig := obj.Type().(*types2.Signature)
2884                 if sig.Recv() != nil {
2885                         return sig.RecvTypeParams()
2886                 }
2887                 return sig.TypeParams()
2888         case *types2.TypeName:
2889                 if !obj.IsAlias() {
2890                         return obj.Type().(*types2.Named).TypeParams()
2891                 }
2892         }
2893         return nil
2894 }
2895
2896 // splitNamed decomposes a use of a defined type into its original
2897 // type definition and the type arguments used to instantiate it.
2898 func splitNamed(typ *types2.Named) (*types2.TypeName, *types2.TypeList) {
2899         base.Assertf(typ.TypeParams().Len() == typ.TypeArgs().Len(), "use of uninstantiated type: %v", typ)
2900
2901         orig := typ.Origin()
2902         base.Assertf(orig.TypeArgs() == nil, "origin %v of %v has type arguments", orig, typ)
2903         base.Assertf(typ.Obj() == orig.Obj(), "%v has object %v, but %v has object %v", typ, typ.Obj(), orig, orig.Obj())
2904
2905         return typ.Obj(), typ.TypeArgs()
2906 }
2907
2908 func asPragmaFlag(p syntax.Pragma) ir.PragmaFlag {
2909         if p == nil {
2910                 return 0
2911         }
2912         return p.(*pragmas).Flag
2913 }
2914
2915 func asWasmImport(p syntax.Pragma) *WasmImport {
2916         if p == nil {
2917                 return nil
2918         }
2919         return p.(*pragmas).WasmImport
2920 }
2921
2922 // isPtrTo reports whether from is the type *to.
2923 func isPtrTo(from, to types2.Type) bool {
2924         ptr, ok := from.(*types2.Pointer)
2925         return ok && types2.Identical(ptr.Elem(), to)
2926 }
2927
2928 // hasFallthrough reports whether stmts ends in a fallthrough
2929 // statement.
2930 func hasFallthrough(stmts []syntax.Stmt) bool {
2931         last, ok := lastNonEmptyStmt(stmts).(*syntax.BranchStmt)
2932         return ok && last.Tok == syntax.Fallthrough
2933 }
2934
2935 // lastNonEmptyStmt returns the last non-empty statement in list, if
2936 // any.
2937 func lastNonEmptyStmt(stmts []syntax.Stmt) syntax.Stmt {
2938         for i := len(stmts) - 1; i >= 0; i-- {
2939                 stmt := stmts[i]
2940                 if _, ok := stmt.(*syntax.EmptyStmt); !ok {
2941                         return stmt
2942                 }
2943         }
2944         return nil
2945 }
2946
2947 // terminates reports whether stmt terminates normal control flow
2948 // (i.e., does not merely advance to the following statement).
2949 func (pw *pkgWriter) terminates(stmt syntax.Stmt) bool {
2950         switch stmt := stmt.(type) {
2951         case *syntax.BranchStmt:
2952                 if stmt.Tok == syntax.Goto {
2953                         return true
2954                 }
2955         case *syntax.ReturnStmt:
2956                 return true
2957         case *syntax.ExprStmt:
2958                 if call, ok := syntax.Unparen(stmt.X).(*syntax.CallExpr); ok {
2959                         if pw.isBuiltin(call.Fun, "panic") {
2960                                 return true
2961                         }
2962                 }
2963
2964                 // The handling of BlockStmt here is approximate, but it serves to
2965                 // allow dead-code elimination for:
2966                 //
2967                 //      if true {
2968                 //              return x
2969                 //      }
2970                 //      unreachable
2971         case *syntax.IfStmt:
2972                 cond := pw.staticBool(&stmt.Cond)
2973                 return (cond < 0 || pw.terminates(stmt.Then)) && (cond > 0 || pw.terminates(stmt.Else))
2974         case *syntax.BlockStmt:
2975                 return pw.terminates(lastNonEmptyStmt(stmt.List))
2976         }
2977
2978         return false
2979 }