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.
16 "cmd/compile/internal/base"
17 "cmd/compile/internal/ir"
18 "cmd/compile/internal/syntax"
19 "cmd/compile/internal/types"
20 "cmd/compile/internal/types2"
23 // This file implements the Unified IR package writer and defines the
24 // Unified IR export data format.
26 // Low-level coding details (e.g., byte-encoding of individual
27 // primitive values, or handling element bitstreams and
28 // cross-references) are handled by internal/pkgbits, so here we only
29 // concern ourselves with higher-level worries like mapping Go
30 // language constructs into elements.
32 // There are two central types in the writing process: the "writer"
33 // type handles writing out individual elements, while the "pkgWriter"
34 // type keeps track of which elements have already been created.
36 // For each sort of "thing" (e.g., position, package, object, type)
37 // that can be written into the export data, there are generally
38 // several methods that work together:
40 // - writer.thing handles writing out a *use* of a thing, which often
41 // means writing a relocation to that thing's encoded index.
43 // - pkgWriter.thingIdx handles reserving an index for a thing, and
44 // writing out any elements needed for the thing.
46 // - writer.doThing handles writing out the *definition* of a thing,
47 // which in general is a mix of low-level coding primitives (e.g.,
48 // ints and strings) or uses of other things.
50 // A design goal of Unified IR is to have a single, canonical writer
51 // implementation, but multiple reader implementations each tailored
52 // to their respective needs. For example, within cmd/compile's own
53 // backend, inlining is implemented largely by just re-running the
54 // function body reading code.
56 // TODO(mdempsky): Add an importer for Unified IR to the x/tools repo,
57 // and better document the file format boundary between public and
60 // A pkgWriter constructs Unified IR export data from the results of
61 // running the types2 type checker on a Go compilation unit.
62 type pkgWriter struct {
66 curpkg *types2.Package
69 // Indices for previously written syntax and types2 things.
71 posBasesIdx map[*syntax.PosBase]pkgbits.Index
72 pkgsIdx map[*types2.Package]pkgbits.Index
73 typsIdx map[types2.Type]pkgbits.Index
74 objsIdx map[types2.Object]pkgbits.Index
76 // Maps from types2.Objects back to their syntax.Decl.
78 funDecls map[*types2.Func]*syntax.FuncDecl
79 typDecls map[*types2.TypeName]typeDeclGen
81 // linknames maps package-scope objects to their linker symbol name,
82 // if specified by a //go:linkname directive.
83 linknames map[types2.Object]string
85 // cgoPragmas accumulates any //go:cgo_* pragmas that need to be
86 // passed through to cmd/link.
90 // newPkgWriter returns an initialized pkgWriter for the specified
92 func newPkgWriter(m posMap, pkg *types2.Package, info *types2.Info) *pkgWriter {
94 PkgEncoder: pkgbits.NewPkgEncoder(base.Debug.SyncFrames),
100 pkgsIdx: make(map[*types2.Package]pkgbits.Index),
101 objsIdx: make(map[types2.Object]pkgbits.Index),
102 typsIdx: make(map[types2.Type]pkgbits.Index),
104 posBasesIdx: make(map[*syntax.PosBase]pkgbits.Index),
106 funDecls: make(map[*types2.Func]*syntax.FuncDecl),
107 typDecls: make(map[*types2.TypeName]typeDeclGen),
109 linknames: make(map[types2.Object]string),
113 // errorf reports a user error about thing p.
114 func (pw *pkgWriter) errorf(p poser, msg string, args ...interface{}) {
115 base.ErrorfAt(pw.m.pos(p), 0, msg, args...)
118 // fatalf reports an internal compiler error about thing p.
119 func (pw *pkgWriter) fatalf(p poser, msg string, args ...interface{}) {
120 base.FatalfAt(pw.m.pos(p), msg, args...)
123 // unexpected reports a fatal error about a thing of unexpected
125 func (pw *pkgWriter) unexpected(what string, p poser) {
126 pw.fatalf(p, "unexpected %s: %v (%T)", what, p, p)
129 func (pw *pkgWriter) typeAndValue(x syntax.Expr) syntax.TypeAndValue {
130 tv, ok := pw.maybeTypeAndValue(x)
132 pw.fatalf(x, "missing Types entry: %v", syntax.String(x))
137 func (pw *pkgWriter) maybeTypeAndValue(x syntax.Expr) (syntax.TypeAndValue, bool) {
138 tv := x.GetTypeInfo()
140 // If x is a generic function whose type arguments are inferred
141 // from assignment context, then we need to find its inferred type
142 // in Info.Instances instead.
143 if name, ok := x.(*syntax.Name); ok {
144 if inst, ok := pw.info.Instances[name]; ok {
149 return tv, tv.Type != nil
152 // typeOf returns the Type of the given value expression.
153 func (pw *pkgWriter) typeOf(expr syntax.Expr) types2.Type {
154 tv := pw.typeAndValue(expr)
156 pw.fatalf(expr, "expected value: %v", syntax.String(expr))
161 // A writer provides APIs for writing out an individual element.
167 // sig holds the signature for the current function body, if any.
168 sig *types2.Signature
170 // TODO(mdempsky): We should be able to prune localsIdx whenever a
171 // scope closes, and then maybe we can just use the same map for
172 // storing the TypeParams too (as their TypeName instead).
174 // localsIdx tracks any local variables declared within this
175 // function body. It's unused for writing out non-body things.
176 localsIdx map[*types2.Var]int
178 // closureVars tracks any free variables that are referenced by this
179 // function body. It's unused for writing out non-body things.
181 closureVarsIdx map[*types2.Var]int // index of previously seen free variables
185 // derived tracks whether the type being written out references any
186 // type parameters. It's unused for writing non-type things.
190 // A writerDict tracks types and objects that are used by a declaration.
191 type writerDict struct {
192 implicits []*types2.TypeName
194 // derived is a slice of type indices for computing derived types
195 // (i.e., types that depend on the declaration's type parameters).
196 derived []derivedInfo
198 // derivedIdx maps a Type to its corresponding index within the
199 // derived slice, if present.
200 derivedIdx map[types2.Type]pkgbits.Index
202 // These slices correspond to entries in the runtime dictionary.
203 typeParamMethodExprs []writerMethodExprInfo
209 type itabInfo struct {
214 // typeParamIndex returns the index of the given type parameter within
215 // the dictionary. This may differ from typ.Index() when there are
216 // implicit type parameters due to defined types declared within a
217 // generic function or method.
218 func (dict *writerDict) typeParamIndex(typ *types2.TypeParam) int {
219 for idx, implicit := range dict.implicits {
220 if implicit.Type().(*types2.TypeParam) == typ {
225 return len(dict.implicits) + typ.Index()
228 // A derivedInfo represents a reference to an encoded generic Go type.
229 type derivedInfo struct {
231 needed bool // TODO(mdempsky): Remove.
234 // A typeInfo represents a reference to an encoded Go type.
236 // If derived is true, then the typeInfo represents a generic Go type
237 // that contains type parameters. In this case, idx is an index into
238 // the readerDict.derived{,Types} arrays.
240 // Otherwise, the typeInfo represents a non-generic Go type, and idx
241 // is an index into the reader.typs array instead.
242 type typeInfo struct {
247 // An objInfo represents a reference to an encoded, instantiated (if
248 // applicable) Go object.
249 type objInfo struct {
250 idx pkgbits.Index // index for the generic function declaration
251 explicits []typeInfo // info for the type arguments
254 // A selectorInfo represents a reference to an encoded field or method
255 // name (i.e., objects that can only be accessed using selector
257 type selectorInfo struct {
259 nameIdx pkgbits.Index
262 // anyDerived reports whether any of info's explicit type arguments
263 // are derived types.
264 func (info objInfo) anyDerived() bool {
265 for _, explicit := range info.explicits {
266 if explicit.derived {
273 // equals reports whether info and other represent the same Go object
274 // (i.e., same base object and identical type arguments, if any).
275 func (info objInfo) equals(other objInfo) bool {
276 if info.idx != other.idx {
279 assert(len(info.explicits) == len(other.explicits))
280 for i, targ := range info.explicits {
281 if targ != other.explicits[i] {
288 type writerMethodExprInfo struct {
290 methodInfo selectorInfo
293 // typeParamMethodExprIdx returns the index where the given encoded
294 // method expression function pointer appears within this dictionary's
295 // type parameters method expressions section, adding it if necessary.
296 func (dict *writerDict) typeParamMethodExprIdx(typeParamIdx int, methodInfo selectorInfo) int {
297 newInfo := writerMethodExprInfo{typeParamIdx, methodInfo}
299 for idx, oldInfo := range dict.typeParamMethodExprs {
300 if oldInfo == newInfo {
305 idx := len(dict.typeParamMethodExprs)
306 dict.typeParamMethodExprs = append(dict.typeParamMethodExprs, newInfo)
310 // subdictIdx returns the index where the given encoded object's
311 // runtime dictionary appears within this dictionary's subdictionary
312 // section, adding it if necessary.
313 func (dict *writerDict) subdictIdx(newInfo objInfo) int {
314 for idx, oldInfo := range dict.subdicts {
315 if oldInfo.equals(newInfo) {
320 idx := len(dict.subdicts)
321 dict.subdicts = append(dict.subdicts, newInfo)
325 // rtypeIdx returns the index where the given encoded type's
326 // *runtime._type value appears within this dictionary's rtypes
327 // section, adding it if necessary.
328 func (dict *writerDict) rtypeIdx(newInfo typeInfo) int {
329 for idx, oldInfo := range dict.rtypes {
330 if oldInfo == newInfo {
335 idx := len(dict.rtypes)
336 dict.rtypes = append(dict.rtypes, newInfo)
340 // itabIdx returns the index where the given encoded type pair's
341 // *runtime.itab value appears within this dictionary's itabs section,
342 // adding it if necessary.
343 func (dict *writerDict) itabIdx(typInfo, ifaceInfo typeInfo) int {
344 newInfo := itabInfo{typInfo, ifaceInfo}
346 for idx, oldInfo := range dict.itabs {
347 if oldInfo == newInfo {
352 idx := len(dict.itabs)
353 dict.itabs = append(dict.itabs, newInfo)
357 func (pw *pkgWriter) newWriter(k pkgbits.RelocKind, marker pkgbits.SyncMarker) *writer {
359 Encoder: pw.NewEncoder(k, marker),
366 // pos writes the position of p into the element bitstream.
367 func (w *writer) pos(p poser) {
368 w.Sync(pkgbits.SyncPos)
371 // TODO(mdempsky): Track down the remaining cases here and fix them.
372 if !w.Bool(pos.IsKnown()) {
376 // TODO(mdempsky): Delta encoding.
377 w.posBase(pos.Base())
382 // posBase writes a reference to the given PosBase into the element
384 func (w *writer) posBase(b *syntax.PosBase) {
385 w.Reloc(pkgbits.RelocPosBase, w.p.posBaseIdx(b))
388 // posBaseIdx returns the index for the given PosBase.
389 func (pw *pkgWriter) posBaseIdx(b *syntax.PosBase) pkgbits.Index {
390 if idx, ok := pw.posBasesIdx[b]; ok {
394 w := pw.newWriter(pkgbits.RelocPosBase, pkgbits.SyncPosBase)
395 w.p.posBasesIdx[b] = w.Idx
397 w.String(trimFilename(b))
399 if !w.Bool(b.IsFileBase()) {
410 // pkg writes a use of the given Package into the element bitstream.
411 func (w *writer) pkg(pkg *types2.Package) {
412 w.pkgRef(w.p.pkgIdx(pkg))
415 func (w *writer) pkgRef(idx pkgbits.Index) {
416 w.Sync(pkgbits.SyncPkg)
417 w.Reloc(pkgbits.RelocPkg, idx)
420 // pkgIdx returns the index for the given package, adding it to the
421 // package export data if needed.
422 func (pw *pkgWriter) pkgIdx(pkg *types2.Package) pkgbits.Index {
423 if idx, ok := pw.pkgsIdx[pkg]; ok {
427 w := pw.newWriter(pkgbits.RelocPkg, pkgbits.SyncPkgDef)
428 pw.pkgsIdx[pkg] = w.Idx
430 // The universe and package unsafe need to be handled specially by
431 // importers anyway, so we serialize them using just their package
432 // path. This ensures that readers don't confuse them for
433 // user-defined packages.
435 case nil: // universe
436 w.String("builtin") // same package path used by godoc
440 // TODO(mdempsky): Write out pkg.Path() for curpkg too.
442 if pkg != w.p.curpkg {
445 base.Assertf(path != "builtin" && path != "unsafe", "unexpected path for user-defined package: %q", path)
449 w.Len(len(pkg.Imports()))
450 for _, imp := range pkg.Imports() {
461 anyTypeName = types2.Universe.Lookup("any").(*types2.TypeName)
462 comparableTypeName = types2.Universe.Lookup("comparable").(*types2.TypeName)
463 runeTypeName = types2.Universe.Lookup("rune").(*types2.TypeName)
466 // typ writes a use of the given type into the bitstream.
467 func (w *writer) typ(typ types2.Type) {
468 w.typInfo(w.p.typIdx(typ, w.dict))
471 // typInfo writes a use of the given type (specified as a typeInfo
472 // instead) into the bitstream.
473 func (w *writer) typInfo(info typeInfo) {
474 w.Sync(pkgbits.SyncType)
475 if w.Bool(info.derived) {
479 w.Reloc(pkgbits.RelocType, info.idx)
483 // typIdx returns the index where the export data description of type
484 // can be read back in. If no such index exists yet, it's created.
486 // typIdx also reports whether typ is a derived type; that is, whether
487 // its identity depends on type parameters.
488 func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
489 if idx, ok := pw.typsIdx[typ]; ok {
490 return typeInfo{idx: idx, derived: false}
493 if idx, ok := dict.derivedIdx[typ]; ok {
494 return typeInfo{idx: idx, derived: true}
498 w := pw.newWriter(pkgbits.RelocType, pkgbits.SyncTypeIdx)
501 switch typ := typ.(type) {
503 base.Fatalf("unexpected type: %v (%T)", typ, typ)
506 switch kind := typ.Kind(); {
507 case kind == types2.Invalid:
508 base.Fatalf("unexpected types2.Invalid")
510 case types2.Typ[kind] == typ:
511 w.Code(pkgbits.TypeBasic)
515 // Handle "byte" and "rune" as references to their TypeNames.
516 obj := types2.Universe.Lookup(typ.Name())
517 assert(obj.Type() == typ)
519 w.Code(pkgbits.TypeNamed)
524 obj, targs := splitNamed(typ)
526 // Defined types that are declared within a generic function (and
527 // thus have implicit type parameters) are always derived types.
528 if w.p.hasImplicitTypeParams(obj) {
532 w.Code(pkgbits.TypeNamed)
535 case *types2.TypeParam:
537 w.Code(pkgbits.TypeTypeParam)
538 w.Len(w.dict.typeParamIndex(typ))
541 w.Code(pkgbits.TypeArray)
542 w.Uint64(uint64(typ.Len()))
546 w.Code(pkgbits.TypeChan)
547 w.Len(int(typ.Dir()))
551 w.Code(pkgbits.TypeMap)
555 case *types2.Pointer:
556 w.Code(pkgbits.TypePointer)
559 case *types2.Signature:
560 base.Assertf(typ.TypeParams() == nil, "unexpected type params: %v", typ)
561 w.Code(pkgbits.TypeSignature)
565 w.Code(pkgbits.TypeSlice)
569 w.Code(pkgbits.TypeStruct)
572 case *types2.Interface:
573 // Handle "any" as reference to its TypeName.
574 if typ == anyTypeName.Type() {
575 w.Code(pkgbits.TypeNamed)
576 w.obj(anyTypeName, nil)
580 w.Code(pkgbits.TypeInterface)
584 w.Code(pkgbits.TypeUnion)
589 idx := pkgbits.Index(len(dict.derived))
590 dict.derived = append(dict.derived, derivedInfo{idx: w.Flush()})
591 dict.derivedIdx[typ] = idx
592 return typeInfo{idx: idx, derived: true}
595 pw.typsIdx[typ] = w.Idx
596 return typeInfo{idx: w.Flush(), derived: false}
599 func (w *writer) structType(typ *types2.Struct) {
600 w.Len(typ.NumFields())
601 for i := 0; i < typ.NumFields(); i++ {
611 func (w *writer) unionType(typ *types2.Union) {
613 for i := 0; i < typ.Len(); i++ {
620 func (w *writer) interfaceType(typ *types2.Interface) {
621 // If typ has no embedded types but it's not a basic interface, then
622 // the natural description we write out below will fail to
624 if typ.NumEmbeddeds() == 0 && !typ.IsMethodSet() {
625 // Currently, this can only happen for the underlying Interface of
626 // "comparable", which is needed to handle type declarations like
627 // "type C comparable".
628 assert(typ == comparableTypeName.Type().(*types2.Named).Underlying())
630 // Export as "interface{ comparable }".
631 w.Len(0) // NumExplicitMethods
632 w.Len(1) // NumEmbeddeds
633 w.Bool(false) // IsImplicit
634 w.typ(comparableTypeName.Type()) // EmbeddedType(0)
638 w.Len(typ.NumExplicitMethods())
639 w.Len(typ.NumEmbeddeds())
641 if typ.NumExplicitMethods() == 0 && typ.NumEmbeddeds() == 1 {
642 w.Bool(typ.IsImplicit())
644 // Implicit interfaces always have 0 explicit methods and 1
645 // embedded type, so we skip writing out the implicit flag
646 // otherwise as a space optimization.
647 assert(!typ.IsImplicit())
650 for i := 0; i < typ.NumExplicitMethods(); i++ {
651 m := typ.ExplicitMethod(i)
652 sig := m.Type().(*types2.Signature)
653 assert(sig.TypeParams() == nil)
660 for i := 0; i < typ.NumEmbeddeds(); i++ {
661 w.typ(typ.EmbeddedType(i))
665 func (w *writer) signature(sig *types2.Signature) {
666 w.Sync(pkgbits.SyncSignature)
667 w.params(sig.Params())
668 w.params(sig.Results())
669 w.Bool(sig.Variadic())
672 func (w *writer) params(typ *types2.Tuple) {
673 w.Sync(pkgbits.SyncParams)
675 for i := 0; i < typ.Len(); i++ {
680 func (w *writer) param(param *types2.Var) {
681 w.Sync(pkgbits.SyncParam)
689 // obj writes a use of the given object into the bitstream.
691 // If obj is a generic object, then explicits are the explicit type
692 // arguments used to instantiate it (i.e., used to substitute the
693 // object's own declared type parameters).
694 func (w *writer) obj(obj types2.Object, explicits *types2.TypeList) {
695 w.objInfo(w.p.objInstIdx(obj, explicits, w.dict))
698 // objInfo writes a use of the given encoded object into the
700 func (w *writer) objInfo(info objInfo) {
701 w.Sync(pkgbits.SyncObject)
702 w.Bool(false) // TODO(mdempsky): Remove; was derived func inst.
703 w.Reloc(pkgbits.RelocObj, info.idx)
705 w.Len(len(info.explicits))
706 for _, info := range info.explicits {
711 // objInstIdx returns the indices for an object and a corresponding
712 // list of type arguments used to instantiate it, adding them to the
713 // export data as needed.
714 func (pw *pkgWriter) objInstIdx(obj types2.Object, explicits *types2.TypeList, dict *writerDict) objInfo {
715 explicitInfos := make([]typeInfo, explicits.Len())
716 for i := range explicitInfos {
717 explicitInfos[i] = pw.typIdx(explicits.At(i), dict)
719 return objInfo{idx: pw.objIdx(obj), explicits: explicitInfos}
722 // objIdx returns the index for the given Object, adding it to the
723 // export data as needed.
724 func (pw *pkgWriter) objIdx(obj types2.Object) pkgbits.Index {
725 // TODO(mdempsky): Validate that obj is a global object (or a local
726 // defined type, which we hoist to global scope anyway).
728 if idx, ok := pw.objsIdx[obj]; ok {
733 derivedIdx: make(map[types2.Type]pkgbits.Index),
736 if isDefinedType(obj) && obj.Pkg() == pw.curpkg {
737 decl, ok := pw.typDecls[obj.(*types2.TypeName)]
739 dict.implicits = decl.implicits
742 // We encode objects into 4 elements across different sections, all
743 // sharing the same index:
745 // - RelocName has just the object's qualified name (i.e.,
746 // Object.Pkg and Object.Name) and the CodeObj indicating what
747 // specific type of Object it is (Var, Func, etc).
749 // - RelocObj has the remaining public details about the object,
750 // relevant to go/types importers.
752 // - RelocObjExt has additional private details about the object,
753 // which are only relevant to cmd/compile itself. This is
754 // separated from RelocObj so that go/types importers are
755 // unaffected by internal compiler changes.
757 // - RelocObjDict has public details about the object's type
758 // parameters and derived type's used by the object. This is
759 // separated to facilitate the eventual introduction of
760 // shape-based stenciling.
762 // TODO(mdempsky): Re-evaluate whether RelocName still makes sense
763 // to keep separate from RelocObj.
765 w := pw.newWriter(pkgbits.RelocObj, pkgbits.SyncObject1)
766 wext := pw.newWriter(pkgbits.RelocObjExt, pkgbits.SyncObject1)
767 wname := pw.newWriter(pkgbits.RelocName, pkgbits.SyncObject1)
768 wdict := pw.newWriter(pkgbits.RelocObjDict, pkgbits.SyncObject1)
770 pw.objsIdx[obj] = w.Idx // break cycles
771 assert(wext.Idx == w.Idx)
772 assert(wname.Idx == w.Idx)
773 assert(wdict.Idx == w.Idx)
778 code := w.doObj(wext, obj)
782 wname.qualifiedIdent(obj)
786 wdict.objDict(obj, w.dict)
792 // doObj writes the RelocObj definition for obj to w, and the
793 // RelocObjExt definition to wext.
794 func (w *writer) doObj(wext *writer, obj types2.Object) pkgbits.CodeObj {
795 if obj.Pkg() != w.p.curpkg {
796 return pkgbits.ObjStub
799 switch obj := obj.(type) {
801 w.p.unexpected("object", obj)
808 return pkgbits.ObjConst
811 decl, ok := w.p.funDecls[obj]
813 sig := obj.Type().(*types2.Signature)
816 w.typeParamNames(sig.TypeParams())
820 return pkgbits.ObjFunc
822 case *types2.TypeName:
826 return pkgbits.ObjAlias
829 named := obj.Type().(*types2.Named)
830 assert(named.TypeArgs() == nil)
833 w.typeParamNames(named.TypeParams())
835 w.typ(named.Underlying())
837 w.Len(named.NumMethods())
838 for i := 0; i < named.NumMethods(); i++ {
839 w.method(wext, named.Method(i))
842 return pkgbits.ObjType
848 return pkgbits.ObjVar
852 // objDict writes the dictionary needed for reading the given object.
853 func (w *writer) objDict(obj types2.Object, dict *writerDict) {
854 // TODO(mdempsky): Split objDict into multiple entries? reader.go
855 // doesn't care about the type parameter bounds, and reader2.go
856 // doesn't care about referenced functions.
858 w.dict = dict // TODO(mdempsky): This is a bit sketchy.
860 w.Len(len(dict.implicits))
862 tparams := objTypeParams(obj)
863 ntparams := tparams.Len()
865 for i := 0; i < ntparams; i++ {
866 w.typ(tparams.At(i).Constraint())
869 nderived := len(dict.derived)
871 for _, typ := range dict.derived {
872 w.Reloc(pkgbits.RelocType, typ.idx)
876 // Write runtime dictionary information.
878 // N.B., the go/types importer reads up to the section, but doesn't
879 // read any further, so it's safe to change. (See TODO above.)
881 // For each type parameter, write out whether the constraint is a
882 // basic interface. This is used to determine how aggressively we
883 // can shape corresponding type arguments.
885 // This is somewhat redundant with writing out the full type
886 // parameter constraints above, but the compiler currently skips
887 // over those. Also, we don't care about the *declared* constraints,
888 // but how the type parameters are actually *used*. E.g., if a type
889 // parameter is constrained to `int | uint` but then never used in
890 // arithmetic/conversions/etc, we could shape those together.
891 for _, implicit := range dict.implicits {
892 tparam := implicit.Type().(*types2.TypeParam)
893 w.Bool(tparam.Underlying().(*types2.Interface).IsMethodSet())
895 for i := 0; i < ntparams; i++ {
896 tparam := tparams.At(i)
897 w.Bool(tparam.Underlying().(*types2.Interface).IsMethodSet())
900 w.Len(len(dict.typeParamMethodExprs))
901 for _, info := range dict.typeParamMethodExprs {
902 w.Len(info.typeParamIdx)
903 w.selectorInfo(info.methodInfo)
906 w.Len(len(dict.subdicts))
907 for _, info := range dict.subdicts {
911 w.Len(len(dict.rtypes))
912 for _, info := range dict.rtypes {
916 w.Len(len(dict.itabs))
917 for _, info := range dict.itabs {
919 w.typInfo(info.iface)
922 assert(len(dict.derived) == nderived)
925 func (w *writer) typeParamNames(tparams *types2.TypeParamList) {
926 w.Sync(pkgbits.SyncTypeParamNames)
928 ntparams := tparams.Len()
929 for i := 0; i < ntparams; i++ {
930 tparam := tparams.At(i).Obj()
936 func (w *writer) method(wext *writer, meth *types2.Func) {
937 decl, ok := w.p.funDecls[meth]
939 sig := meth.Type().(*types2.Signature)
941 w.Sync(pkgbits.SyncMethod)
944 w.typeParamNames(sig.RecvTypeParams())
948 w.pos(decl) // XXX: Hack to workaround linker limitations.
952 // qualifiedIdent writes out the name of an object declared at package
953 // scope. (For now, it's also used to refer to local defined types.)
954 func (w *writer) qualifiedIdent(obj types2.Object) {
955 w.Sync(pkgbits.SyncSym)
958 if isDefinedType(obj) && obj.Pkg() == w.p.curpkg {
959 decl, ok := w.p.typDecls[obj.(*types2.TypeName)]
962 // For local defined types, we embed a scope-disambiguation
963 // number directly into their name. types.SplitVargenSuffix then
964 // knows to look for this.
966 // TODO(mdempsky): Find a better solution; this is terrible.
967 name = fmt.Sprintf("%s·%v", name, decl.gen)
975 // TODO(mdempsky): We should be able to omit pkg from both localIdent
976 // and selector, because they should always be known from context.
977 // However, past frustrations with this optimization in iexport make
978 // me a little nervous to try it again.
980 // localIdent writes the name of a locally declared object (i.e.,
981 // objects that can only be accessed by non-qualified name, within the
982 // context of a particular function).
983 func (w *writer) localIdent(obj types2.Object) {
984 assert(!isGlobal(obj))
985 w.Sync(pkgbits.SyncLocalIdent)
990 // selector writes the name of a field or method (i.e., objects that
991 // can only be accessed using selector expressions).
992 func (w *writer) selector(obj types2.Object) {
993 w.selectorInfo(w.p.selectorIdx(obj))
996 func (w *writer) selectorInfo(info selectorInfo) {
997 w.Sync(pkgbits.SyncSelector)
998 w.pkgRef(info.pkgIdx)
999 w.StringRef(info.nameIdx)
1002 func (pw *pkgWriter) selectorIdx(obj types2.Object) selectorInfo {
1003 pkgIdx := pw.pkgIdx(obj.Pkg())
1004 nameIdx := pw.StringIdx(obj.Name())
1005 return selectorInfo{pkgIdx: pkgIdx, nameIdx: nameIdx}
1008 // @@@ Compiler extensions
1010 func (w *writer) funcExt(obj *types2.Func) {
1011 decl, ok := w.p.funDecls[obj]
1014 // TODO(mdempsky): Extend these pragma validation flags to account
1015 // for generics. E.g., linkname probably doesn't make sense at
1018 pragma := asPragmaFlag(decl.Pragma)
1019 if pragma&ir.Systemstack != 0 && pragma&ir.Nosplit != 0 {
1020 w.p.errorf(decl, "go:nosplit and go:systemstack cannot be combined")
1022 wi := asWasmImport(decl.Pragma)
1024 if decl.Body != nil {
1025 if pragma&ir.Noescape != 0 {
1026 w.p.errorf(decl, "can only use //go:noescape with external func implementations")
1029 w.p.errorf(decl, "can only use //go:wasmimport with external func implementations")
1031 if (pragma&ir.UintptrKeepAlive != 0 && pragma&ir.UintptrEscapes == 0) && pragma&ir.Nosplit == 0 {
1032 // Stack growth can't handle uintptr arguments that may
1033 // be pointers (as we don't know which are pointers
1034 // when creating the stack map). Thus uintptrkeepalive
1035 // functions (and all transitive callees) must be
1038 // N.B. uintptrescapes implies uintptrkeepalive but it
1039 // is OK since the arguments must escape to the heap.
1041 // TODO(prattmic): Add recursive nosplit check of callees.
1042 // TODO(prattmic): Functions with no body (i.e.,
1043 // assembly) must also be nosplit, but we can't check
1045 w.p.errorf(decl, "go:uintptrkeepalive requires go:nosplit")
1048 if base.Flag.Complete || decl.Name.Value == "init" {
1049 // Linknamed functions are allowed to have no body. Hopefully
1050 // the linkname target has a body. See issue 23311.
1051 // Wasmimport functions are also allowed to have no body.
1052 if _, ok := w.p.linknames[obj]; !ok && wi == nil {
1053 w.p.errorf(decl, "missing function body")
1058 sig, block := obj.Type().(*types2.Signature), decl.Body
1059 body, closureVars := w.p.bodyIdx(sig, block, w.dict)
1060 if len(closureVars) > 0 {
1061 fmt.Fprintln(os.Stderr, "CLOSURE", closureVars)
1063 assert(len(closureVars) == 0)
1065 w.Sync(pkgbits.SyncFuncExt)
1066 w.pragmaFlag(pragma)
1069 if buildcfg.GOARCH == "wasm" {
1079 w.Bool(false) // stub extension
1080 w.Reloc(pkgbits.RelocBody, body)
1081 w.Sync(pkgbits.SyncEOF)
1084 func (w *writer) typeExt(obj *types2.TypeName) {
1085 decl, ok := w.p.typDecls[obj]
1088 w.Sync(pkgbits.SyncTypeExt)
1090 w.pragmaFlag(asPragmaFlag(decl.Pragma))
1092 // No LSym.SymIdx info yet.
1097 func (w *writer) varExt(obj *types2.Var) {
1098 w.Sync(pkgbits.SyncVarExt)
1102 func (w *writer) linkname(obj types2.Object) {
1103 w.Sync(pkgbits.SyncLinkname)
1105 w.String(w.p.linknames[obj])
1108 func (w *writer) pragmaFlag(p ir.PragmaFlag) {
1109 w.Sync(pkgbits.SyncPragma)
1113 // @@@ Function bodies
1115 // bodyIdx returns the index for the given function body (specified by
1116 // block), adding it to the export data
1117 func (pw *pkgWriter) bodyIdx(sig *types2.Signature, block *syntax.BlockStmt, dict *writerDict) (idx pkgbits.Index, closureVars []posVar) {
1118 w := pw.newWriter(pkgbits.RelocBody, pkgbits.SyncFuncBody)
1122 w.declareParams(sig)
1123 if w.Bool(block != nil) {
1128 return w.Flush(), w.closureVars
1131 func (w *writer) declareParams(sig *types2.Signature) {
1132 addLocals := func(params *types2.Tuple) {
1133 for i := 0; i < params.Len(); i++ {
1134 w.addLocal(params.At(i))
1138 if recv := sig.Recv(); recv != nil {
1141 addLocals(sig.Params())
1142 addLocals(sig.Results())
1145 // addLocal records the declaration of a new local variable.
1146 func (w *writer) addLocal(obj *types2.Var) {
1147 idx := len(w.localsIdx)
1149 w.Sync(pkgbits.SyncAddLocal)
1150 if w.p.SyncMarkers() {
1155 if w.localsIdx == nil {
1156 w.localsIdx = make(map[*types2.Var]int)
1158 w.localsIdx[obj] = idx
1161 // useLocal writes a reference to the given local or free variable
1162 // into the bitstream.
1163 func (w *writer) useLocal(pos syntax.Pos, obj *types2.Var) {
1164 w.Sync(pkgbits.SyncUseObjLocal)
1166 if idx, ok := w.localsIdx[obj]; w.Bool(ok) {
1171 idx, ok := w.closureVarsIdx[obj]
1173 if w.closureVarsIdx == nil {
1174 w.closureVarsIdx = make(map[*types2.Var]int)
1176 idx = len(w.closureVars)
1177 w.closureVars = append(w.closureVars, posVar{pos, obj})
1178 w.closureVarsIdx[obj] = idx
1183 func (w *writer) openScope(pos syntax.Pos) {
1184 w.Sync(pkgbits.SyncOpenScope)
1188 func (w *writer) closeScope(pos syntax.Pos) {
1189 w.Sync(pkgbits.SyncCloseScope)
1191 w.closeAnotherScope()
1194 func (w *writer) closeAnotherScope() {
1195 w.Sync(pkgbits.SyncCloseAnotherScope)
1200 // stmt writes the given statement into the function body bitstream.
1201 func (w *writer) stmt(stmt syntax.Stmt) {
1202 var stmts []syntax.Stmt
1204 stmts = []syntax.Stmt{stmt}
1209 func (w *writer) stmts(stmts []syntax.Stmt) {
1211 w.Sync(pkgbits.SyncStmts)
1212 for _, stmt := range stmts {
1214 // Any statements after a terminating statement are safe to
1215 // omit, at least until the next labeled statement.
1216 if _, ok := stmt.(*syntax.LabeledStmt); !ok {
1221 dead = w.p.terminates(stmt)
1224 w.Sync(pkgbits.SyncStmtsEnd)
1227 func (w *writer) stmt1(stmt syntax.Stmt) {
1228 switch stmt := stmt.(type) {
1230 w.p.unexpected("statement", stmt)
1232 case nil, *syntax.EmptyStmt:
1235 case *syntax.AssignStmt:
1237 case stmt.Rhs == nil:
1239 w.op(binOps[stmt.Op])
1243 case stmt.Op != 0 && stmt.Op != syntax.Def:
1244 w.Code(stmtAssignOp)
1245 w.op(binOps[stmt.Op])
1250 if stmt.Op != syntax.Shl && stmt.Op != syntax.Shr {
1251 typ = w.p.typeOf(stmt.Lhs)
1253 w.implicitConvExpr(typ, stmt.Rhs)
1256 w.assignStmt(stmt, stmt.Lhs, stmt.Rhs)
1259 case *syntax.BlockStmt:
1263 case *syntax.BranchStmt:
1266 w.op(branchOps[stmt.Tok])
1267 w.optLabel(stmt.Label)
1269 case *syntax.CallStmt:
1272 w.op(callOps[stmt.Tok])
1274 if stmt.Tok == syntax.Defer {
1275 w.optExpr(stmt.DeferAt)
1278 case *syntax.DeclStmt:
1279 for _, decl := range stmt.DeclList {
1283 case *syntax.ExprStmt:
1287 case *syntax.ForStmt:
1291 case *syntax.IfStmt:
1295 case *syntax.LabeledStmt:
1301 case *syntax.ReturnStmt:
1305 resultTypes := w.sig.Results()
1306 dstType := func(i int) types2.Type {
1307 return resultTypes.At(i).Type()
1309 w.multiExpr(stmt, dstType, syntax.UnpackListExpr(stmt.Results))
1311 case *syntax.SelectStmt:
1315 case *syntax.SendStmt:
1316 chanType := types2.CoreType(w.p.typeOf(stmt.Chan)).(*types2.Chan)
1321 w.implicitConvExpr(chanType.Elem(), stmt.Value)
1323 case *syntax.SwitchStmt:
1329 func (w *writer) assignList(expr syntax.Expr) {
1330 exprs := syntax.UnpackListExpr(expr)
1333 for _, expr := range exprs {
1338 func (w *writer) assign(expr syntax.Expr) {
1339 expr = syntax.Unparen(expr)
1341 if name, ok := expr.(*syntax.Name); ok {
1342 if name.Value == "_" {
1347 if obj, ok := w.p.info.Defs[name]; ok {
1348 obj := obj.(*types2.Var)
1355 // TODO(mdempsky): Minimize locals index size by deferring
1356 // this until the variables actually come into scope.
1366 func (w *writer) declStmt(decl syntax.Decl) {
1367 switch decl := decl.(type) {
1369 w.p.unexpected("declaration", decl)
1371 case *syntax.ConstDecl, *syntax.TypeDecl:
1373 case *syntax.VarDecl:
1374 w.assignStmt(decl, namesAsExpr(decl.NameList), decl.Values)
1378 // assignStmt writes out an assignment for "lhs = rhs".
1379 func (w *writer) assignStmt(pos poser, lhs0, rhs0 syntax.Expr) {
1380 lhs := syntax.UnpackListExpr(lhs0)
1381 rhs := syntax.UnpackListExpr(rhs0)
1386 // As if w.assignList(lhs0).
1388 for _, expr := range lhs {
1392 dstType := func(i int) types2.Type {
1395 // Finding dstType is somewhat involved, because for VarDecl
1396 // statements, the Names are only added to the info.{Defs,Uses}
1397 // maps, not to info.Types.
1398 if name, ok := syntax.Unparen(dst).(*syntax.Name); ok {
1399 if name.Value == "_" {
1400 return nil // ok: no implicit conversion
1401 } else if def, ok := w.p.info.Defs[name].(*types2.Var); ok {
1403 } else if use, ok := w.p.info.Uses[name].(*types2.Var); ok {
1406 w.p.fatalf(dst, "cannot find type of destination object: %v", dst)
1410 return w.p.typeOf(dst)
1413 w.multiExpr(pos, dstType, rhs)
1416 func (w *writer) blockStmt(stmt *syntax.BlockStmt) {
1417 w.Sync(pkgbits.SyncBlockStmt)
1418 w.openScope(stmt.Pos())
1420 w.closeScope(stmt.Rbrace)
1423 func (w *writer) forStmt(stmt *syntax.ForStmt) {
1424 w.Sync(pkgbits.SyncForStmt)
1425 w.openScope(stmt.Pos())
1427 if rang, ok := stmt.Init.(*syntax.RangeClause); w.Bool(ok) {
1429 w.assignList(rang.Lhs)
1432 xtyp := w.p.typeOf(rang.X)
1433 if _, isMap := types2.CoreType(xtyp).(*types2.Map); isMap {
1437 lhs := syntax.UnpackListExpr(rang.Lhs)
1438 assign := func(i int, src types2.Type) {
1442 dst := syntax.Unparen(lhs[i])
1443 if name, ok := dst.(*syntax.Name); ok && name.Value == "_" {
1447 var dstType types2.Type
1449 // For `:=` assignments, the LHS names only appear in Defs,
1450 // not Types (as used by typeOf).
1451 dstType = w.p.info.Defs[dst.(*syntax.Name)].(*types2.Var).Type()
1453 dstType = w.p.typeOf(dst)
1456 w.convRTTI(src, dstType)
1459 keyType, valueType := types2.RangeKeyVal(w.p.typeOf(rang.X))
1461 assign(1, valueType)
1465 if stmt.Cond != nil && w.p.staticBool(&stmt.Cond) < 0 { // always false
1467 stmt.Body.List = nil
1472 w.optExpr(stmt.Cond)
1476 w.blockStmt(stmt.Body)
1477 w.Bool(w.distinctVars(stmt))
1478 w.closeAnotherScope()
1481 func (w *writer) distinctVars(stmt *syntax.ForStmt) bool {
1482 lv := base.Debug.LoopVar
1483 fileVersion := w.p.info.FileVersions[stmt.Pos().Base()]
1484 is122 := fileVersion == "" || version.Compare(fileVersion, "go1.22") >= 0
1486 // Turning off loopvar for 1.22 is only possible with loopvarhash=qn
1488 // Debug.LoopVar values to be preserved for 1.21 compatibility are 1 and 2,
1489 // which are also set (=1) by GOEXPERIMENT=loopvar. The knobs for turning on
1490 // the new, unshared, loopvar behavior apply to versions less than 1.21 because
1491 // (1) 1.21 also did that and (2) this is believed to be the likely use case;
1492 // anyone checking to see if it affects their code will just run the GOEXPERIMENT
1493 // but will not also update all their go.mod files to 1.21.
1495 // -gcflags=-d=loopvar=3 enables logging for 1.22 but does not turn loopvar on for <= 1.21.
1497 return is122 || lv > 0 && lv != 3
1500 func (w *writer) ifStmt(stmt *syntax.IfStmt) {
1501 cond := w.p.staticBool(&stmt.Cond)
1503 w.Sync(pkgbits.SyncIfStmt)
1504 w.openScope(stmt.Pos())
1510 w.blockStmt(stmt.Then)
1512 w.pos(stmt.Then.Rbrace)
1517 w.closeAnotherScope()
1520 func (w *writer) selectStmt(stmt *syntax.SelectStmt) {
1521 w.Sync(pkgbits.SyncSelectStmt)
1524 w.Len(len(stmt.Body))
1525 for i, clause := range stmt.Body {
1527 w.closeScope(clause.Pos())
1529 w.openScope(clause.Pos())
1533 w.stmts(clause.Body)
1535 if len(stmt.Body) > 0 {
1536 w.closeScope(stmt.Rbrace)
1540 func (w *writer) switchStmt(stmt *syntax.SwitchStmt) {
1541 w.Sync(pkgbits.SyncSwitchStmt)
1543 w.openScope(stmt.Pos())
1547 var iface, tagType types2.Type
1548 if guard, ok := stmt.Tag.(*syntax.TypeSwitchGuard); w.Bool(ok) {
1549 iface = w.p.typeOf(guard.X)
1552 if tag := guard.Lhs; w.Bool(tag != nil) {
1555 // Like w.localIdent, but we don't have a types2.Object.
1556 w.Sync(pkgbits.SyncLocalIdent)
1564 var tagValue constant.Value
1566 tv := w.p.typeAndValue(tag)
1570 tagType = types2.Typ[types2.Bool]
1571 tagValue = constant.MakeBool(true)
1574 if tagValue != nil {
1575 // If the switch tag has a constant value, look for a case
1576 // clause that we always branch to.
1578 var target *syntax.CaseClause
1580 for _, clause := range stmt.Body {
1581 if clause.Cases == nil {
1584 for _, cas := range syntax.UnpackListExpr(clause.Cases) {
1585 tv := w.p.typeAndValue(cas)
1586 if tv.Value == nil {
1587 return // non-constant case; give up
1589 if constant.Compare(tagValue, token.EQL, tv.Value) {
1595 // We've found the target clause, if any.
1598 if hasFallthrough(target.Body) {
1599 return // fallthrough is tricky; give up
1602 // Rewrite as single "default" case.
1604 stmt.Body = []*syntax.CaseClause{target}
1609 // Clear switch tag (i.e., replace with implicit "true").
1612 tagType = types2.Typ[types2.Bool]
1616 // Walk is going to emit comparisons between the tag value and
1617 // each case expression, and we want these comparisons to always
1618 // have the same type. If there are any case values that can't be
1619 // converted to the tag value's type, then convert everything to
1622 for _, clause := range stmt.Body {
1623 for _, cas := range syntax.UnpackListExpr(clause.Cases) {
1624 if casType := w.p.typeOf(cas); !types2.AssignableTo(casType, tagType) {
1625 tagType = types2.NewInterfaceType(nil, nil)
1631 if w.Bool(tag != nil) {
1632 w.implicitConvExpr(tagType, tag)
1636 w.Len(len(stmt.Body))
1637 for i, clause := range stmt.Body {
1639 w.closeScope(clause.Pos())
1641 w.openScope(clause.Pos())
1645 cases := syntax.UnpackListExpr(clause.Cases)
1648 for _, cas := range cases {
1649 if w.Bool(isNil(w.p, cas)) {
1652 w.exprType(iface, cas)
1655 // As if w.exprList(clause.Cases),
1656 // but with implicit conversions to tagType.
1658 w.Sync(pkgbits.SyncExprList)
1659 w.Sync(pkgbits.SyncExprs)
1661 for _, cas := range cases {
1662 w.implicitConvExpr(tagType, cas)
1666 if obj, ok := w.p.info.Implicits[clause]; ok {
1667 // TODO(mdempsky): These pos details are quirkish, but also
1668 // necessary so the variable's position is correct for DWARF
1669 // scope assignment later. It would probably be better for us to
1670 // instead just set the variable's DWARF scoping info earlier so
1671 // we can give it the correct position information.
1673 if typs := syntax.UnpackListExpr(clause.Cases); len(typs) != 0 {
1674 pos = typeExprEndPos(typs[len(typs)-1])
1678 obj := obj.(*types2.Var)
1683 w.stmts(clause.Body)
1685 if len(stmt.Body) > 0 {
1686 w.closeScope(stmt.Rbrace)
1689 w.closeScope(stmt.Rbrace)
1692 func (w *writer) label(label *syntax.Name) {
1693 w.Sync(pkgbits.SyncLabel)
1695 // TODO(mdempsky): Replace label strings with dense indices.
1696 w.String(label.Value)
1699 func (w *writer) optLabel(label *syntax.Name) {
1700 w.Sync(pkgbits.SyncOptLabel)
1701 if w.Bool(label != nil) {
1708 // expr writes the given expression into the function body bitstream.
1709 func (w *writer) expr(expr syntax.Expr) {
1710 base.Assertf(expr != nil, "missing expression")
1712 expr = syntax.Unparen(expr) // skip parens; unneeded after typecheck
1714 obj, inst := lookupObj(w.p, expr)
1715 targs := inst.TypeArgs
1717 if tv, ok := w.p.maybeTypeAndValue(expr); ok {
1719 w.p.fatalf(expr, "unexpected type expression %v", syntax.String(expr))
1722 if tv.Value != nil {
1725 typ := idealType(tv)
1732 if _, isNil := obj.(*types2.Nil); isNil {
1739 // With shape types (and particular pointer shaping), we may have
1740 // an expression of type "go.shape.*uint8", but need to reshape it
1741 // to another shape-identical type to allow use in field
1742 // selection, indexing, etc.
1743 if typ := tv.Type; !tv.IsBuiltin() && !isTuple(typ) && !isUntyped(typ) {
1751 if targs.Len() != 0 {
1752 obj := obj.(*types2.Func)
1754 w.Code(exprFuncInst)
1756 w.funcInst(obj, targs)
1766 obj := obj.(*types2.Var)
1767 assert(!obj.IsField())
1770 w.useLocal(expr.Pos(), obj)
1774 switch expr := expr.(type) {
1776 w.p.unexpected("expression", expr)
1778 case *syntax.CompositeLit:
1782 case *syntax.FuncLit:
1786 case *syntax.SelectorExpr:
1787 sel, ok := w.p.info.Selections[expr]
1792 w.p.fatalf(expr, "unexpected selection kind: %v", sel.Kind())
1794 case types2.FieldVal:
1795 w.Code(exprFieldVal)
1798 w.selector(sel.Obj())
1800 case types2.MethodVal:
1801 w.Code(exprMethodVal)
1802 typ := w.recvExpr(expr, sel)
1804 w.methodExpr(expr, typ, sel)
1806 case types2.MethodExpr:
1807 w.Code(exprMethodExpr)
1809 tv := w.p.typeAndValue(expr.X)
1812 index := sel.Index()
1813 implicits := index[:len(index)-1]
1818 w.Len(len(implicits))
1819 for _, ix := range implicits {
1821 typ = deref2(typ).Underlying().(*types2.Struct).Field(ix).Type()
1824 recv := sel.Obj().(*types2.Func).Type().(*types2.Signature).Recv().Type()
1825 if w.Bool(isPtrTo(typ, recv)) { // need deref
1827 } else if w.Bool(isPtrTo(recv, typ)) { // need addr
1832 w.methodExpr(expr, typ, sel)
1835 case *syntax.IndexExpr:
1836 _ = w.p.typeOf(expr.Index) // ensure this is an index expression, not an instantiation
1838 xtyp := w.p.typeOf(expr.X)
1840 var keyType types2.Type
1841 if mapType, ok := types2.CoreType(xtyp).(*types2.Map); ok {
1842 keyType = mapType.Key()
1848 w.implicitConvExpr(keyType, expr.Index)
1853 case *syntax.SliceExpr:
1857 for _, n := range &expr.Index {
1861 case *syntax.AssertExpr:
1862 iface := w.p.typeOf(expr.X)
1867 w.exprType(iface, expr.Type)
1870 case *syntax.Operation:
1873 w.op(unOps[expr.Op])
1879 var commonType types2.Type
1881 case syntax.Shl, syntax.Shr:
1882 // ok: operands are allowed to have different types
1884 xtyp := w.p.typeOf(expr.X)
1885 ytyp := w.p.typeOf(expr.Y)
1887 case types2.AssignableTo(xtyp, ytyp):
1889 case types2.AssignableTo(ytyp, xtyp):
1892 w.p.fatalf(expr, "failed to find common type between %v and %v", xtyp, ytyp)
1896 w.Code(exprBinaryOp)
1897 w.op(binOps[expr.Op])
1898 w.implicitConvExpr(commonType, expr.X)
1900 w.implicitConvExpr(commonType, expr.Y)
1902 case *syntax.CallExpr:
1903 tv := w.p.typeAndValue(expr.Fun)
1905 assert(len(expr.ArgList) == 1)
1906 assert(!expr.HasDots)
1907 w.convertExpr(tv.Type, expr.ArgList[0], false)
1911 var rtype types2.Type
1913 switch obj, _ := lookupObj(w.p, syntax.Unparen(expr.Fun)); obj.Name() {
1915 assert(len(expr.ArgList) >= 1)
1916 assert(!expr.HasDots)
1920 w.exprType(nil, expr.ArgList[0])
1921 w.exprs(expr.ArgList[1:])
1923 typ := w.p.typeOf(expr)
1924 switch coreType := types2.CoreType(typ).(type) {
1926 w.p.fatalf(expr, "unexpected core type: %v", coreType)
1932 w.rtype(sliceElem(typ))
1938 assert(len(expr.ArgList) == 1)
1939 assert(!expr.HasDots)
1943 w.exprType(nil, expr.ArgList[0])
1947 assert(len(expr.ArgList) == 1)
1948 assert(!expr.HasDots)
1952 w.typ(w.p.typeOf(expr.ArgList[0]))
1956 assert(len(expr.ArgList) == 1)
1957 assert(!expr.HasDots)
1961 w.typ(w.p.typeOf(expr.ArgList[0]))
1965 assert(len(expr.ArgList) == 1)
1966 assert(!expr.HasDots)
1967 selector := syntax.Unparen(expr.ArgList[0]).(*syntax.SelectorExpr)
1968 index := w.p.info.Selections[selector].Index()
1970 w.Code(exprOffsetof)
1972 w.typ(deref2(w.p.typeOf(selector.X)))
1973 w.Len(len(index) - 1)
1974 for _, idx := range index {
1980 rtype = sliceElem(w.p.typeOf(expr))
1982 typ := w.p.typeOf(expr.ArgList[0])
1983 if tuple, ok := typ.(*types2.Tuple); ok { // "copy(g())"
1984 typ = tuple.At(0).Type()
1986 rtype = sliceElem(typ)
1988 typ := w.p.typeOf(expr.ArgList[0])
1989 if tuple, ok := typ.(*types2.Tuple); ok { // "delete(g())"
1990 typ = tuple.At(0).Type()
1994 rtype = sliceElem(w.p.typeOf(expr))
1998 writeFunExpr := func() {
1999 fun := syntax.Unparen(expr.Fun)
2001 if selector, ok := fun.(*syntax.SelectorExpr); ok {
2002 if sel, ok := w.p.info.Selections[selector]; ok && sel.Kind() == types2.MethodVal {
2003 w.Bool(true) // method call
2004 typ := w.recvExpr(selector, sel)
2005 w.methodExpr(selector, typ, sel)
2010 w.Bool(false) // not a method call (i.e., normal function call)
2012 if obj, inst := lookupObj(w.p, fun); w.Bool(obj != nil && inst.TypeArgs.Len() != 0) {
2013 obj := obj.(*types2.Func)
2016 w.funcInst(obj, inst.TypeArgs)
2023 sigType := types2.CoreType(tv.Type).(*types2.Signature)
2024 paramTypes := sigType.Params()
2030 paramType := func(i int) types2.Type {
2031 if sigType.Variadic() && !expr.HasDots && i >= paramTypes.Len()-1 {
2032 return paramTypes.At(paramTypes.Len() - 1).Type().(*types2.Slice).Elem()
2034 return paramTypes.At(i).Type()
2037 w.multiExpr(expr, paramType, expr.ArgList)
2038 w.Bool(expr.HasDots)
2045 func sliceElem(typ types2.Type) types2.Type {
2046 return types2.CoreType(typ).(*types2.Slice).Elem()
2049 func (w *writer) optExpr(expr syntax.Expr) {
2050 if w.Bool(expr != nil) {
2055 // recvExpr writes out expr.X, but handles any implicit addressing,
2056 // dereferencing, and field selections appropriate for the method
2058 func (w *writer) recvExpr(expr *syntax.SelectorExpr, sel *types2.Selection) types2.Type {
2059 index := sel.Index()
2060 implicits := index[:len(index)-1]
2065 w.Len(len(implicits))
2067 typ := w.p.typeOf(expr.X)
2068 for _, ix := range implicits {
2069 typ = deref2(typ).Underlying().(*types2.Struct).Field(ix).Type()
2073 recv := sel.Obj().(*types2.Func).Type().(*types2.Signature).Recv().Type()
2074 if w.Bool(isPtrTo(typ, recv)) { // needs deref
2076 } else if w.Bool(isPtrTo(recv, typ)) { // needs addr
2083 // funcInst writes a reference to an instantiated function.
2084 func (w *writer) funcInst(obj *types2.Func, targs *types2.TypeList) {
2085 info := w.p.objInstIdx(obj, targs, w.dict)
2087 // Type arguments list contains derived types; we can emit a static
2088 // call to the shaped function, but need to dynamically compute the
2089 // runtime dictionary pointer.
2090 if w.Bool(info.anyDerived()) {
2091 w.Len(w.dict.subdictIdx(info))
2095 // Type arguments list is statically known; we can emit a static
2096 // call with a statically reference to the respective runtime
2101 // methodExpr writes out a reference to the method selected by
2102 // expr. sel should be the corresponding types2.Selection, and recv
2103 // the type produced after any implicit addressing, dereferencing, and
2104 // field selection. (Note: recv might differ from sel.Obj()'s receiver
2105 // parameter in the case of interface types, and is needed for
2106 // handling type parameter methods.)
2107 func (w *writer) methodExpr(expr *syntax.SelectorExpr, recv types2.Type, sel *types2.Selection) {
2108 fun := sel.Obj().(*types2.Func)
2109 sig := fun.Type().(*types2.Signature)
2116 // Method on a type parameter. These require an indirect call
2117 // through the current function's runtime dictionary.
2118 if typeParam, ok := recv.(*types2.TypeParam); w.Bool(ok) {
2119 typeParamIdx := w.dict.typeParamIndex(typeParam)
2120 methodInfo := w.p.selectorIdx(fun)
2122 w.Len(w.dict.typeParamMethodExprIdx(typeParamIdx, methodInfo))
2126 if isInterface(recv) != isInterface(sig.Recv().Type()) {
2127 w.p.fatalf(expr, "isInterface inconsistency: %v and %v", recv, sig.Recv().Type())
2130 if !isInterface(recv) {
2131 if named, ok := deref2(recv).(*types2.Named); ok {
2132 obj, targs := splitNamed(named)
2133 info := w.p.objInstIdx(obj, targs, w.dict)
2135 // Method on a derived receiver type. These can be handled by a
2136 // static call to the shaped method, but require dynamically
2137 // looking up the appropriate dictionary argument in the current
2138 // function's runtime dictionary.
2139 if w.p.hasImplicitTypeParams(obj) || info.anyDerived() {
2140 w.Bool(true) // dynamic subdictionary
2141 w.Len(w.dict.subdictIdx(info))
2145 // Method on a fully known receiver type. These can be handled
2146 // by a static call to the shaped method, and with a static
2147 // reference to the receiver type's dictionary.
2148 if targs.Len() != 0 {
2149 w.Bool(false) // no dynamic subdictionary
2150 w.Bool(true) // static dictionary
2157 w.Bool(false) // no dynamic subdictionary
2158 w.Bool(false) // no static dictionary
2161 // multiExpr writes a sequence of expressions, where the i'th value is
2162 // implicitly converted to dstType(i). It also handles when exprs is a
2163 // single, multi-valued expression (e.g., the multi-valued argument in
2164 // an f(g()) call, or the RHS operand in a comma-ok assignment).
2165 func (w *writer) multiExpr(pos poser, dstType func(int) types2.Type, exprs []syntax.Expr) {
2166 w.Sync(pkgbits.SyncMultiExpr)
2168 if len(exprs) == 1 {
2170 if tuple, ok := w.p.typeOf(expr).(*types2.Tuple); ok {
2171 assert(tuple.Len() > 1)
2172 w.Bool(true) // N:1 assignment
2177 for i := 0; i < tuple.Len(); i++ {
2178 src := tuple.At(i).Type()
2179 // TODO(mdempsky): Investigate not writing src here. I think
2180 // the reader should be able to infer it from expr anyway.
2182 if dst := dstType(i); w.Bool(dst != nil && !types2.Identical(src, dst)) {
2183 if src == nil || dst == nil {
2184 w.p.fatalf(pos, "src is %v, dst is %v", src, dst)
2186 if !types2.AssignableTo(src, dst) {
2187 w.p.fatalf(pos, "%v is not assignable to %v", src, dst)
2190 w.convRTTI(src, dst)
2197 w.Bool(false) // N:N assignment
2199 for i, expr := range exprs {
2200 w.implicitConvExpr(dstType(i), expr)
2204 // implicitConvExpr is like expr, but if dst is non-nil and different
2205 // from expr's type, then an implicit conversion operation is inserted
2206 // at expr's position.
2207 func (w *writer) implicitConvExpr(dst types2.Type, expr syntax.Expr) {
2208 w.convertExpr(dst, expr, true)
2211 func (w *writer) convertExpr(dst types2.Type, expr syntax.Expr, implicit bool) {
2212 src := w.p.typeOf(expr)
2214 // Omit implicit no-op conversions.
2215 identical := dst == nil || types2.Identical(src, dst)
2216 if implicit && identical {
2221 if implicit && !types2.AssignableTo(src, dst) {
2222 w.p.fatalf(expr, "%v is not assignable to %v", src, dst)
2229 w.convRTTI(src, dst)
2230 w.Bool(isTypeParam(dst))
2235 func (w *writer) compLit(lit *syntax.CompositeLit) {
2236 typ := w.p.typeOf(lit)
2238 w.Sync(pkgbits.SyncCompLit)
2242 if ptr, ok := types2.CoreType(typ).(*types2.Pointer); ok {
2245 var keyType, elemType types2.Type
2246 var structType *types2.Struct
2247 switch typ0 := typ; typ := types2.CoreType(typ).(type) {
2249 w.p.fatalf(lit, "unexpected composite literal type: %v", typ)
2251 elemType = typ.Elem()
2254 keyType, elemType = typ.Key(), typ.Elem()
2256 elemType = typ.Elem()
2257 case *types2.Struct:
2261 w.Len(len(lit.ElemList))
2262 for i, elem := range lit.ElemList {
2263 elemType := elemType
2264 if structType != nil {
2265 if kv, ok := elem.(*syntax.KeyValueExpr); ok {
2266 // use position of expr.Key rather than of elem (which has position of ':')
2268 i = fieldIndex(w.p.info, structType, kv.Key.(*syntax.Name))
2273 elemType = structType.Field(i).Type()
2276 if kv, ok := elem.(*syntax.KeyValueExpr); w.Bool(ok) {
2277 // use position of expr.Key rather than of elem (which has position of ':')
2279 w.implicitConvExpr(keyType, kv.Key)
2284 w.implicitConvExpr(elemType, elem)
2288 func (w *writer) funcLit(expr *syntax.FuncLit) {
2289 sig := w.p.typeOf(expr).(*types2.Signature)
2291 body, closureVars := w.p.bodyIdx(sig, expr.Body, w.dict)
2293 w.Sync(pkgbits.SyncFuncLit)
2297 w.Len(len(closureVars))
2298 for _, cv := range closureVars {
2300 w.useLocal(cv.pos, cv.var_)
2303 w.Reloc(pkgbits.RelocBody, body)
2306 type posVar struct {
2311 func (p posVar) String() string {
2312 return p.pos.String() + ":" + p.var_.String()
2315 func (w *writer) exprList(expr syntax.Expr) {
2316 w.Sync(pkgbits.SyncExprList)
2317 w.exprs(syntax.UnpackListExpr(expr))
2320 func (w *writer) exprs(exprs []syntax.Expr) {
2321 w.Sync(pkgbits.SyncExprs)
2323 for _, expr := range exprs {
2328 // rtype writes information so that the reader can construct an
2329 // expression of type *runtime._type representing typ.
2330 func (w *writer) rtype(typ types2.Type) {
2331 typ = types2.Default(typ)
2333 info := w.p.typIdx(typ, w.dict)
2337 func (w *writer) rtypeInfo(info typeInfo) {
2338 w.Sync(pkgbits.SyncRType)
2340 if w.Bool(info.derived) {
2341 w.Len(w.dict.rtypeIdx(info))
2347 // varDictIndex writes out information for populating DictIndex for
2348 // the ir.Name that will represent obj.
2349 func (w *writer) varDictIndex(obj *types2.Var) {
2350 info := w.p.typIdx(obj.Type(), w.dict)
2351 if w.Bool(info.derived) {
2352 w.Len(w.dict.rtypeIdx(info))
2356 func isUntyped(typ types2.Type) bool {
2357 basic, ok := typ.(*types2.Basic)
2358 return ok && basic.Info()&types2.IsUntyped != 0
2361 func isTuple(typ types2.Type) bool {
2362 _, ok := typ.(*types2.Tuple)
2366 func (w *writer) itab(typ, iface types2.Type) {
2367 typ = types2.Default(typ)
2368 iface = types2.Default(iface)
2370 typInfo := w.p.typIdx(typ, w.dict)
2371 ifaceInfo := w.p.typIdx(iface, w.dict)
2373 w.rtypeInfo(typInfo)
2374 w.rtypeInfo(ifaceInfo)
2375 if w.Bool(typInfo.derived || ifaceInfo.derived) {
2376 w.Len(w.dict.itabIdx(typInfo, ifaceInfo))
2380 // convRTTI writes information so that the reader can construct
2381 // expressions for converting from src to dst.
2382 func (w *writer) convRTTI(src, dst types2.Type) {
2383 w.Sync(pkgbits.SyncConvRTTI)
2387 func (w *writer) exprType(iface types2.Type, typ syntax.Expr) {
2388 base.Assertf(iface == nil || isInterface(iface), "%v must be nil or an interface type", iface)
2390 tv := w.p.typeAndValue(typ)
2393 w.Sync(pkgbits.SyncExprType)
2396 if w.Bool(iface != nil && !iface.Underlying().(*types2.Interface).Empty()) {
2397 w.itab(tv.Type, iface)
2401 info := w.p.typIdx(tv.Type, w.dict)
2402 w.Bool(info.derived)
2406 // isInterface reports whether typ is known to be an interface type.
2407 // If typ is a type parameter, then isInterface reports an internal
2408 // compiler error instead.
2409 func isInterface(typ types2.Type) bool {
2410 if _, ok := typ.(*types2.TypeParam); ok {
2411 // typ is a type parameter and may be instantiated as either a
2412 // concrete or interface type, so the writer can't depend on
2414 base.Fatalf("%v is a type parameter", typ)
2417 _, ok := typ.Underlying().(*types2.Interface)
2421 // op writes an Op into the bitstream.
2422 func (w *writer) op(op ir.Op) {
2423 // TODO(mdempsky): Remove in favor of explicit codes? Would make
2424 // export data more stable against internal refactorings, but low
2425 // priority at the moment.
2427 w.Sync(pkgbits.SyncOp)
2431 // @@@ Package initialization
2433 // Caution: This code is still clumsy, because toolstash -cmp is
2434 // particularly sensitive to it.
2436 type typeDeclGen struct {
2440 // Implicit type parameters in scope at this type declaration.
2441 implicits []*types2.TypeName
2444 type fileImports struct {
2445 importedEmbed, importedUnsafe bool
2448 // declCollector is a visitor type that collects compiler-needed
2449 // information about declarations that types2 doesn't track.
2451 // Notably, it maps declared types and functions back to their
2452 // declaration statement, keeps track of implicit type parameters, and
2453 // assigns unique type "generation" numbers to local defined types.
2454 type declCollector struct {
2459 implicits []*types2.TypeName
2462 func (c *declCollector) withTParams(obj types2.Object) *declCollector {
2463 tparams := objTypeParams(obj)
2470 copy.implicits = copy.implicits[:len(copy.implicits):len(copy.implicits)]
2471 for i := 0; i < n; i++ {
2472 copy.implicits = append(copy.implicits, tparams.At(i).Obj())
2477 func (c *declCollector) Visit(n syntax.Node) syntax.Visitor {
2480 switch n := n.(type) {
2482 pw.checkPragmas(n.Pragma, ir.GoBuildPragma, false)
2484 case *syntax.ImportDecl:
2485 pw.checkPragmas(n.Pragma, 0, false)
2487 switch pkgNameOf(pw.info, n).Imported().Path() {
2489 c.file.importedEmbed = true
2491 c.file.importedUnsafe = true
2494 case *syntax.ConstDecl:
2495 pw.checkPragmas(n.Pragma, 0, false)
2497 case *syntax.FuncDecl:
2498 pw.checkPragmas(n.Pragma, funcPragmas, false)
2500 obj := pw.info.Defs[n.Name].(*types2.Func)
2501 pw.funDecls[obj] = n
2503 return c.withTParams(obj)
2505 case *syntax.TypeDecl:
2506 obj := pw.info.Defs[n.Name].(*types2.TypeName)
2507 d := typeDeclGen{TypeDecl: n, implicits: c.implicits}
2510 pw.checkPragmas(n.Pragma, 0, false)
2512 pw.checkPragmas(n.Pragma, 0, false)
2514 // Assign a unique ID to function-scoped defined types.
2521 pw.typDecls[obj] = d
2523 // TODO(mdempsky): Omit? Not strictly necessary; only matters for
2524 // type declarations within function literals within parameterized
2525 // type declarations, but types2 the function literals will be
2526 // constant folded away.
2527 return c.withTParams(obj)
2529 case *syntax.VarDecl:
2530 pw.checkPragmas(n.Pragma, 0, true)
2532 if p, ok := n.Pragma.(*pragmas); ok && len(p.Embeds) > 0 {
2533 if err := checkEmbed(n, c.file.importedEmbed, c.withinFunc); err != nil {
2534 pw.errorf(p.Embeds[0].Pos, "%s", err)
2538 case *syntax.BlockStmt:
2541 copy.withinFunc = true
2549 func (pw *pkgWriter) collectDecls(noders []*noder) {
2551 for _, p := range noders {
2552 var file fileImports
2554 syntax.Walk(p.file, &declCollector{
2560 pw.cgoPragmas = append(pw.cgoPragmas, p.pragcgobuf...)
2562 for _, l := range p.linknames {
2563 if !file.importedUnsafe {
2564 pw.errorf(l.pos, "//go:linkname only allowed in Go files that import \"unsafe\"")
2568 switch obj := pw.curpkg.Scope().Lookup(l.local).(type) {
2569 case *types2.Func, *types2.Var:
2570 if _, ok := pw.linknames[obj]; !ok {
2571 pw.linknames[obj] = l.remote
2573 pw.errorf(l.pos, "duplicate //go:linkname for %s", l.local)
2577 if types.AllowsGoVersion(1, 18) {
2578 pw.errorf(l.pos, "//go:linkname must refer to declared function or variable")
2585 func (pw *pkgWriter) checkPragmas(p syntax.Pragma, allowed ir.PragmaFlag, embedOK bool) {
2589 pragma := p.(*pragmas)
2591 for _, pos := range pragma.Pos {
2592 if pos.Flag&^allowed != 0 {
2593 pw.errorf(pos.Pos, "misplaced compiler directive")
2598 for _, e := range pragma.Embeds {
2599 pw.errorf(e.Pos, "misplaced go:embed directive")
2604 func (w *writer) pkgInit(noders []*noder) {
2605 w.Len(len(w.p.cgoPragmas))
2606 for _, cgoPragma := range w.p.cgoPragmas {
2607 w.Strings(cgoPragma)
2612 w.Sync(pkgbits.SyncDecls)
2613 for _, p := range noders {
2614 for _, decl := range p.file.DeclList {
2620 w.Sync(pkgbits.SyncEOF)
2623 func (w *writer) pkgInitOrder() {
2624 // TODO(mdempsky): Write as a function body instead?
2625 w.Len(len(w.p.info.InitOrder))
2626 for _, init := range w.p.info.InitOrder {
2627 w.Len(len(init.Lhs))
2628 for _, v := range init.Lhs {
2635 func (w *writer) pkgDecl(decl syntax.Decl) {
2636 switch decl := decl.(type) {
2638 w.p.unexpected("declaration", decl)
2640 case *syntax.ImportDecl:
2642 case *syntax.ConstDecl:
2644 w.pkgObjs(decl.NameList...)
2646 case *syntax.FuncDecl:
2647 if decl.Name.Value == "_" {
2648 break // skip blank functions
2651 obj := w.p.info.Defs[decl.Name].(*types2.Func)
2652 sig := obj.Type().(*types2.Signature)
2654 if sig.RecvTypeParams() != nil || sig.TypeParams() != nil {
2655 break // skip generic functions
2658 if recv := sig.Recv(); recv != nil {
2660 w.typ(recvBase(recv))
2666 w.pkgObjs(decl.Name)
2668 case *syntax.TypeDecl:
2669 if len(decl.TParamList) != 0 {
2670 break // skip generic type decls
2673 if decl.Name.Value == "_" {
2674 break // skip blank type decls
2677 name := w.p.info.Defs[decl.Name].(*types2.TypeName)
2678 // Skip type declarations for interfaces that are only usable as
2679 // type parameter bounds.
2680 if iface, ok := name.Type().Underlying().(*types2.Interface); ok && !iface.IsMethodSet() {
2685 w.pkgObjs(decl.Name)
2687 case *syntax.VarDecl:
2689 w.pkgObjs(decl.NameList...)
2691 var embeds []pragmaEmbed
2692 if p, ok := decl.Pragma.(*pragmas); ok {
2696 for _, embed := range embeds {
2698 w.Strings(embed.Patterns)
2703 func (w *writer) pkgObjs(names ...*syntax.Name) {
2704 w.Sync(pkgbits.SyncDeclNames)
2707 for _, name := range names {
2708 obj, ok := w.p.info.Defs[name]
2711 w.Sync(pkgbits.SyncDeclName)
2718 // staticBool analyzes a boolean expression and reports whether it's
2719 // always true (positive result), always false (negative result), or
2722 // It also simplifies the expression while preserving semantics, if
2724 func (pw *pkgWriter) staticBool(ep *syntax.Expr) int {
2725 if val := pw.typeAndValue(*ep).Value; val != nil {
2726 if constant.BoolVal(val) {
2733 if e, ok := (*ep).(*syntax.Operation); ok {
2736 return pw.staticBool(&e.X)
2739 x := pw.staticBool(&e.X)
2745 y := pw.staticBool(&e.Y)
2747 if pw.typeAndValue(e.X).Value != nil {
2754 x := pw.staticBool(&e.X)
2760 y := pw.staticBool(&e.Y)
2762 if pw.typeAndValue(e.X).Value != nil {
2773 // hasImplicitTypeParams reports whether obj is a defined type with
2774 // implicit type parameters (e.g., declared within a generic function
2776 func (pw *pkgWriter) hasImplicitTypeParams(obj *types2.TypeName) bool {
2777 if obj.Pkg() == pw.curpkg {
2778 decl, ok := pw.typDecls[obj]
2780 if len(decl.implicits) != 0 {
2787 // isDefinedType reports whether obj is a defined type.
2788 func isDefinedType(obj types2.Object) bool {
2789 if obj, ok := obj.(*types2.TypeName); ok {
2790 return !obj.IsAlias()
2795 // isGlobal reports whether obj was declared at package scope.
2797 // Caveat: blank objects are not declared.
2798 func isGlobal(obj types2.Object) bool {
2799 return obj.Parent() == obj.Pkg().Scope()
2802 // lookupObj returns the object that expr refers to, if any. If expr
2803 // is an explicit instantiation of a generic object, then the instance
2804 // object is returned as well.
2805 func lookupObj(p *pkgWriter, expr syntax.Expr) (obj types2.Object, inst types2.Instance) {
2806 if index, ok := expr.(*syntax.IndexExpr); ok {
2807 args := syntax.UnpackListExpr(index.Index)
2809 tv := p.typeAndValue(args[0])
2811 return // normal index expression
2818 // Strip package qualifier, if present.
2819 if sel, ok := expr.(*syntax.SelectorExpr); ok {
2820 if !isPkgQual(p.info, sel) {
2821 return // normal selector expression
2826 if name, ok := expr.(*syntax.Name); ok {
2827 obj = p.info.Uses[name]
2828 inst = p.info.Instances[name]
2833 // isPkgQual reports whether the given selector expression is a
2834 // package-qualified identifier.
2835 func isPkgQual(info *types2.Info, sel *syntax.SelectorExpr) bool {
2836 if name, ok := sel.X.(*syntax.Name); ok {
2837 _, isPkgName := info.Uses[name].(*types2.PkgName)
2843 // isNil reports whether expr is a (possibly parenthesized) reference
2844 // to the predeclared nil value.
2845 func isNil(p *pkgWriter, expr syntax.Expr) bool {
2846 tv := p.typeAndValue(expr)
2850 // isBuiltin reports whether expr is a (possibly parenthesized)
2851 // referenced to the specified built-in function.
2852 func (pw *pkgWriter) isBuiltin(expr syntax.Expr, builtin string) bool {
2853 if name, ok := syntax.Unparen(expr).(*syntax.Name); ok && name.Value == builtin {
2854 return pw.typeAndValue(name).IsBuiltin()
2859 // recvBase returns the base type for the given receiver parameter.
2860 func recvBase(recv *types2.Var) *types2.Named {
2862 if ptr, ok := typ.(*types2.Pointer); ok {
2865 return typ.(*types2.Named)
2868 // namesAsExpr returns a list of names as a syntax.Expr.
2869 func namesAsExpr(names []*syntax.Name) syntax.Expr {
2870 if len(names) == 1 {
2874 exprs := make([]syntax.Expr, len(names))
2875 for i, name := range names {
2878 return &syntax.ListExpr{ElemList: exprs}
2881 // fieldIndex returns the index of the struct field named by key.
2882 func fieldIndex(info *types2.Info, str *types2.Struct, key *syntax.Name) int {
2883 field := info.Uses[key].(*types2.Var)
2885 for i := 0; i < str.NumFields(); i++ {
2886 if str.Field(i) == field {
2891 panic(fmt.Sprintf("%s: %v is not a field of %v", key.Pos(), field, str))
2894 // objTypeParams returns the type parameters on the given object.
2895 func objTypeParams(obj types2.Object) *types2.TypeParamList {
2896 switch obj := obj.(type) {
2898 sig := obj.Type().(*types2.Signature)
2899 if sig.Recv() != nil {
2900 return sig.RecvTypeParams()
2902 return sig.TypeParams()
2903 case *types2.TypeName:
2905 return obj.Type().(*types2.Named).TypeParams()
2911 // splitNamed decomposes a use of a defined type into its original
2912 // type definition and the type arguments used to instantiate it.
2913 func splitNamed(typ *types2.Named) (*types2.TypeName, *types2.TypeList) {
2914 base.Assertf(typ.TypeParams().Len() == typ.TypeArgs().Len(), "use of uninstantiated type: %v", typ)
2916 orig := typ.Origin()
2917 base.Assertf(orig.TypeArgs() == nil, "origin %v of %v has type arguments", orig, typ)
2918 base.Assertf(typ.Obj() == orig.Obj(), "%v has object %v, but %v has object %v", typ, typ.Obj(), orig, orig.Obj())
2920 return typ.Obj(), typ.TypeArgs()
2923 func asPragmaFlag(p syntax.Pragma) ir.PragmaFlag {
2927 return p.(*pragmas).Flag
2930 func asWasmImport(p syntax.Pragma) *WasmImport {
2934 return p.(*pragmas).WasmImport
2937 // isPtrTo reports whether from is the type *to.
2938 func isPtrTo(from, to types2.Type) bool {
2939 ptr, ok := from.(*types2.Pointer)
2940 return ok && types2.Identical(ptr.Elem(), to)
2943 // hasFallthrough reports whether stmts ends in a fallthrough
2945 func hasFallthrough(stmts []syntax.Stmt) bool {
2946 last, ok := lastNonEmptyStmt(stmts).(*syntax.BranchStmt)
2947 return ok && last.Tok == syntax.Fallthrough
2950 // lastNonEmptyStmt returns the last non-empty statement in list, if
2952 func lastNonEmptyStmt(stmts []syntax.Stmt) syntax.Stmt {
2953 for i := len(stmts) - 1; i >= 0; i-- {
2955 if _, ok := stmt.(*syntax.EmptyStmt); !ok {
2962 // terminates reports whether stmt terminates normal control flow
2963 // (i.e., does not merely advance to the following statement).
2964 func (pw *pkgWriter) terminates(stmt syntax.Stmt) bool {
2965 switch stmt := stmt.(type) {
2966 case *syntax.BranchStmt:
2967 if stmt.Tok == syntax.Goto {
2970 case *syntax.ReturnStmt:
2972 case *syntax.ExprStmt:
2973 if call, ok := syntax.Unparen(stmt.X).(*syntax.CallExpr); ok {
2974 if pw.isBuiltin(call.Fun, "panic") {
2979 // The handling of BlockStmt here is approximate, but it serves to
2980 // allow dead-code elimination for:
2986 case *syntax.IfStmt:
2987 cond := pw.staticBool(&stmt.Cond)
2988 return (cond < 0 || pw.terminates(stmt.Then)) && (cond > 0 || pw.terminates(stmt.Else))
2989 case *syntax.BlockStmt:
2990 return pw.terminates(lastNonEmptyStmt(stmt.List))