Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// UNREVIEWED
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package noder
import (
"fmt"
"go/constant"
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/compile/internal/syntax"
"cmd/compile/internal/types2"
)
type pkgWriter struct {
pkgEncoder
m posMap
curpkg *types2.Package
info *types2.Info
posBasesIdx map[*syntax.PosBase]int
pkgsIdx map[*types2.Package]int
typsIdx map[types2.Type]int
globalsIdx map[types2.Object]int
funDecls map[*types2.Func]*syntax.FuncDecl
typDecls map[*types2.TypeName]typeDeclGen
linknames map[types2.Object]string
cgoPragmas [][]string
dups dupTypes
}
func newPkgWriter(m posMap, pkg *types2.Package, info *types2.Info) *pkgWriter {
return &pkgWriter{
pkgEncoder: newPkgEncoder(),
m: m,
curpkg: pkg,
info: info,
pkgsIdx: make(map[*types2.Package]int),
globalsIdx: make(map[types2.Object]int),
typsIdx: make(map[types2.Type]int),
posBasesIdx: make(map[*syntax.PosBase]int),
funDecls: make(map[*types2.Func]*syntax.FuncDecl),
typDecls: make(map[*types2.TypeName]typeDeclGen),
linknames: make(map[types2.Object]string),
}
}
func (pw *pkgWriter) errorf(p poser, msg string, args ...interface{}) {
base.ErrorfAt(pw.m.pos(p), msg, args...)
}
func (pw *pkgWriter) fatalf(p poser, msg string, args ...interface{}) {
base.FatalfAt(pw.m.pos(p), msg, args...)
}
func (pw *pkgWriter) unexpected(what string, p poser) {
pw.fatalf(p, "unexpected %s: %v (%T)", what, p, p)
}
type writer struct {
p *pkgWriter
encoder
// For writing out object descriptions, ext points to the extension
// writer for where we can write the compiler's private extension
// details for the object.
//
// TODO(mdempsky): This is a little hacky, but works easiest with
// the way things are currently.
ext *writer
// TODO(mdempsky): We should be able to prune localsIdx whenever a
// scope closes, and then maybe we can just use the same map for
// storing the TypeParams too (as their TypeName instead).
// variables declared within this function
localsIdx map[*types2.Var]int
closureVars []posObj
closureVarsIdx map[*types2.Var]int
dict *writerDict
derived bool
}
// A writerDict tracks types and objects that are used by a declaration.
type writerDict struct {
implicits []*types2.TypeName
// derived is a slice of type indices for computing derived types
// (i.e., types that depend on the declaration's type parameters).
Matthew Dempsky
committed
derived []derivedInfo
// derivedIdx maps a Type to its corresponding index within the
// derived slice, if present.
derivedIdx map[types2.Type]int
Matthew Dempsky
committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// funcs lists references to generic functions that were
// instantiated with derived types (i.e., that require
// sub-dictionaries when called at run time).
funcs []objInfo
}
type derivedInfo struct {
idx int
needed bool
}
type typeInfo struct {
idx int
derived bool
}
type objInfo struct {
idx int // index for the generic function declaration
explicits []typeInfo // info for the type arguments
}
func (info objInfo) anyDerived() bool {
for _, explicit := range info.explicits {
if explicit.derived {
return true
}
}
return false
}
func (info objInfo) equals(other objInfo) bool {
if info.idx != other.idx {
return false
}
assert(len(info.explicits) == len(other.explicits))
for i, targ := range info.explicits {
if targ != other.explicits[i] {
return false
}
}
return true
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
}
func (pw *pkgWriter) newWriter(k reloc, marker syncMarker) *writer {
return &writer{
encoder: pw.newEncoder(k, marker),
p: pw,
}
}
// @@@ Positions
func (w *writer) pos(p poser) {
w.sync(syncPos)
pos := p.Pos()
// TODO(mdempsky): Track down the remaining cases here and fix them.
if !w.bool(pos.IsKnown()) {
return
}
// TODO(mdempsky): Delta encoding. Also, if there's a b-side, update
// its position base too (but not vice versa!).
w.posBase(pos.Base())
w.uint(pos.Line())
w.uint(pos.Col())
}
func (w *writer) posBase(b *syntax.PosBase) {
w.reloc(relocPosBase, w.p.posBaseIdx(b))
}
func (pw *pkgWriter) posBaseIdx(b *syntax.PosBase) int {
if idx, ok := pw.posBasesIdx[b]; ok {
return idx
}
w := pw.newWriter(relocPosBase, syncPosBase)
w.p.posBasesIdx[b] = w.idx
// TODO(mdempsky): What exactly does "fileh" do anyway? Is writing
// out both of these strings really the right thing to do here?
fn := b.Filename()
w.string(fn)
w.string(fileh(fn))
if !w.bool(b.IsFileBase()) {
w.pos(b)
w.uint(b.Line())
w.uint(b.Col())
}
return w.flush()
}
// @@@ Packages
func (w *writer) pkg(pkg *types2.Package) {
w.sync(syncPkg)
w.reloc(relocPkg, w.p.pkgIdx(pkg))
}
func (pw *pkgWriter) pkgIdx(pkg *types2.Package) int {
if idx, ok := pw.pkgsIdx[pkg]; ok {
return idx
}
w := pw.newWriter(relocPkg, syncPkgDef)
pw.pkgsIdx[pkg] = w.idx
if pkg == nil {
w.string("builtin")
} else {
var path string
if pkg != w.p.curpkg {
path = pkg.Path()
}
w.string(path)
w.string(pkg.Name())
w.len(pkg.Height())
w.len(len(pkg.Imports()))
for _, imp := range pkg.Imports() {
w.pkg(imp)
}
}
return w.flush()
}
// @@@ Types
func (w *writer) typ(typ types2.Type) {
Matthew Dempsky
committed
w.typInfo(w.p.typIdx(typ, w.dict))
}
Matthew Dempsky
committed
func (w *writer) typInfo(info typeInfo) {
w.sync(syncType)
Matthew Dempsky
committed
if w.bool(info.derived) {
w.len(info.idx)
w.derived = true
} else {
Matthew Dempsky
committed
w.reloc(relocType, info.idx)
}
}
// typIdx returns the index where the export data description of type
// can be read back in. If no such index exists yet, it's created.
//
// typIdx also reports whether typ is a derived type; that is, whether
// its identity depends on type parameters.
Matthew Dempsky
committed
func (pw *pkgWriter) typIdx(typ types2.Type, dict *writerDict) typeInfo {
if quirksMode() {
typ = pw.dups.orig(typ)
}
if idx, ok := pw.typsIdx[typ]; ok {
Matthew Dempsky
committed
return typeInfo{idx: idx, derived: false}
}
if dict != nil {
if idx, ok := dict.derivedIdx[typ]; ok {
Matthew Dempsky
committed
return typeInfo{idx: idx, derived: true}
}
}
w := pw.newWriter(relocType, syncTypeIdx)
w.dict = dict
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
switch typ := typ.(type) {
default:
base.Fatalf("unexpected type: %v (%T)", typ, typ)
case *types2.Basic:
if kind := typ.Kind(); types2.Typ[kind] == typ {
w.code(typeBasic)
w.len(int(kind))
break
}
// Handle "byte" and "rune" as references to their TypeName.
obj := types2.Universe.Lookup(typ.Name())
assert(obj.Type() == typ)
w.code(typeNamed)
w.obj(obj, nil)
case *types2.Named:
// Type aliases can refer to uninstantiated generic types, so we
// might see len(TParams) != 0 && len(TArgs) == 0 here.
// TODO(mdempsky): Revisit after #46477 is resolved.
assert(len(typ.TParams()) == len(typ.TArgs()) || len(typ.TArgs()) == 0)
// TODO(mdempsky): Why do we need to loop here?
orig := typ
for orig.TArgs() != nil {
orig = orig.Orig()
}
w.code(typeNamed)
w.obj(orig.Obj(), typ.TArgs())
case *types2.TypeParam:
index := func() int {
for idx, name := range w.dict.implicits {
if name.Type().(*types2.TypeParam) == typ {
return idx
}
}
return len(w.dict.implicits) + typ.Index()
}()
w.derived = true
w.code(typeTypeParam)
w.len(index)
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
case *types2.Array:
w.code(typeArray)
w.uint64(uint64(typ.Len()))
w.typ(typ.Elem())
case *types2.Chan:
w.code(typeChan)
w.len(int(typ.Dir()))
w.typ(typ.Elem())
case *types2.Map:
w.code(typeMap)
w.typ(typ.Key())
w.typ(typ.Elem())
case *types2.Pointer:
w.code(typePointer)
w.typ(typ.Elem())
case *types2.Signature:
assert(typ.TParams() == nil)
w.code(typeSignature)
w.signature(typ)
case *types2.Slice:
w.code(typeSlice)
w.typ(typ.Elem())
case *types2.Struct:
w.code(typeStruct)
w.structType(typ)
case *types2.Interface:
w.code(typeInterface)
w.interfaceType(typ)
case *types2.Union:
w.code(typeUnion)
w.unionType(typ)
}
if w.derived {
idx := len(dict.derived)
Matthew Dempsky
committed
dict.derived = append(dict.derived, derivedInfo{idx: w.flush()})
dict.derivedIdx[typ] = idx
Matthew Dempsky
committed
return typeInfo{idx: idx, derived: true}
}
pw.typsIdx[typ] = w.idx
Matthew Dempsky
committed
return typeInfo{idx: w.flush(), derived: false}
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
}
func (w *writer) structType(typ *types2.Struct) {
w.len(typ.NumFields())
for i := 0; i < typ.NumFields(); i++ {
f := typ.Field(i)
w.pos(f)
w.selector(f)
w.typ(f.Type())
w.string(typ.Tag(i))
w.bool(f.Embedded())
}
}
func (w *writer) unionType(typ *types2.Union) {
w.len(typ.NumTerms())
for i := 0; i < typ.NumTerms(); i++ {
term, tilde := typ.Term(i)
w.typ(term)
w.bool(tilde)
}
}
func (w *writer) interfaceType(typ *types2.Interface) {
w.len(typ.NumExplicitMethods())
w.len(typ.NumEmbeddeds())
for i := 0; i < typ.NumExplicitMethods(); i++ {
m := typ.ExplicitMethod(i)
sig := m.Type().(*types2.Signature)
assert(sig.TParams() == nil)
w.pos(m)
w.selector(m)
w.signature(sig)
}
for i := 0; i < typ.NumEmbeddeds(); i++ {
w.typ(typ.EmbeddedType(i))
}
}
func (w *writer) signature(sig *types2.Signature) {
w.sync(syncSignature)
w.params(sig.Params())
w.params(sig.Results())
w.bool(sig.Variadic())
}
func (w *writer) params(typ *types2.Tuple) {
w.sync(syncParams)
w.len(typ.Len())
for i := 0; i < typ.Len(); i++ {
w.param(typ.At(i))
}
}
func (w *writer) param(param *types2.Var) {
w.sync(syncParam)
w.pos(param)
w.localIdent(param)
w.typ(param.Type())
}
// @@@ Objects
func (w *writer) obj(obj types2.Object, explicits []types2.Type) {
Matthew Dempsky
committed
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
explicitInfos := make([]typeInfo, len(explicits))
for i, explicit := range explicits {
explicitInfos[i] = w.p.typIdx(explicit, w.dict)
}
info := objInfo{idx: w.p.objIdx(obj), explicits: explicitInfos}
if _, ok := obj.(*types2.Func); ok && info.anyDerived() {
idx := -1
for i, prev := range w.dict.funcs {
if prev.equals(info) {
idx = i
}
}
if idx < 0 {
idx = len(w.dict.funcs)
w.dict.funcs = append(w.dict.funcs, info)
}
// TODO(mdempsky): Push up into expr; this shouldn't appear
// outside of expression context.
w.sync(syncObject)
w.bool(true)
w.len(idx)
return
}
// TODO(mdempsky): Push up into typIdx; this shouldn't be needed
// except while writing out types.
if isDefinedType(obj) && obj.Pkg() == w.p.curpkg {
decl, ok := w.p.typDecls[obj.(*types2.TypeName)]
assert(ok)
if len(decl.implicits) != 0 {
w.derived = true
}
w.sync(syncObject)
Matthew Dempsky
committed
w.bool(false)
w.reloc(relocObj, info.idx)
Matthew Dempsky
committed
w.len(len(info.explicits))
for _, info := range info.explicits {
w.typInfo(info)
func (pw *pkgWriter) objIdx(obj types2.Object) int {
if idx, ok := pw.globalsIdx[obj]; ok {
return idx
}
dict := &writerDict{
derivedIdx: make(map[types2.Type]int),
}
if isDefinedType(obj) && obj.Pkg() == pw.curpkg {
decl, ok := pw.typDecls[obj.(*types2.TypeName)]
assert(ok)
dict.implicits = decl.implicits
}
w := pw.newWriter(relocObj, syncObject1)
w.ext = pw.newWriter(relocObjExt, syncObject1)
wdict := pw.newWriter(relocObjDict, syncObject1)
pw.globalsIdx[obj] = w.idx // break cycles
assert(w.ext.idx == w.idx)
assert(wdict.idx == w.idx)
w.dict = dict
w.ext.dict = dict
// Ident goes first so importer can avoid unnecessary work if
// they've already resolved this object.
w.qualifiedIdent(obj)
w.typeParamBounds(objTypeParams(obj))
w.doObj(obj)
w.flush()
w.ext.flush()
// Done writing out the object description; write out the list of
Matthew Dempsky
committed
// derived types and instantiated functions found along the way.
wdict.len(len(dict.derived))
for _, typ := range dict.derived {
Matthew Dempsky
committed
wdict.reloc(relocType, typ.idx)
wdict.bool(typ.needed)
}
wdict.len(len(dict.funcs))
for _, fn := range dict.funcs {
wdict.reloc(relocObj, fn.idx)
wdict.len(len(fn.explicits))
for _, targ := range fn.explicits {
wdict.typInfo(targ)
}
}
wdict.flush()
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
return w.idx
}
func (w *writer) doObj(obj types2.Object) {
if obj.Pkg() != w.p.curpkg {
w.code(objStub)
return
}
switch obj := obj.(type) {
default:
w.p.unexpected("object", obj)
case *types2.Const:
w.code(objConst)
w.pos(obj)
w.value(obj.Type(), obj.Val())
case *types2.Func:
decl, ok := w.p.funDecls[obj]
assert(ok)
sig := obj.Type().(*types2.Signature)
// Rewrite blank methods into blank functions.
// They aren't included in the receiver type's method set,
// and we still want to write them out to be compiled
// for regression tests.
// TODO(mdempsky): Change regress tests to avoid relying
// on blank functions/methods, so we can just ignore them
// altogether.
if recv := sig.Recv(); recv != nil {
assert(obj.Name() == "_")
assert(sig.TParams() == nil)
params := make([]*types2.Var, 1+sig.Params().Len())
params[0] = recv
for i := 0; i < sig.Params().Len(); i++ {
params[1+i] = sig.Params().At(i)
}
sig = types2.NewSignature(nil, types2.NewTuple(params...), sig.Results(), sig.Variadic())
}
w.code(objFunc)
w.pos(obj)
w.typeParamNames(sig.TParams())
w.signature(sig)
w.pos(decl)
w.ext.funcExt(obj)
case *types2.TypeName:
decl, ok := w.p.typDecls[obj]
assert(ok)
if obj.IsAlias() {
w.code(objAlias)
w.pos(obj)
w.typ(obj.Type())
break
}
named := obj.Type().(*types2.Named)
assert(named.TArgs() == nil)
w.code(objType)
w.pos(obj)
w.typeParamNames(named.TParams())
w.ext.typeExt(obj)
w.typExpr(decl.Type)
w.len(named.NumMethods())
for i := 0; i < named.NumMethods(); i++ {
w.method(named.Method(i))
}
case *types2.Var:
w.code(objVar)
w.pos(obj)
w.typ(obj.Type())
w.ext.varExt(obj)
}
}
// typExpr writes the type represented by the given expression.
func (w *writer) typExpr(expr syntax.Expr) {
tv, ok := w.p.info.Types[expr]
assert(ok)
assert(tv.IsType())
w.typ(tv.Type)
}
func (w *writer) value(typ types2.Type, val constant.Value) {
w.sync(syncValue)
w.typ(typ)
w.rawValue(val)
}
func (w *writer) typeParamBounds(tparams []*types2.TypeName) {
w.sync(syncTypeParamBounds)
w.len(len(w.dict.implicits))
w.len(len(tparams))
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
for _, tparam := range tparams {
w.typ(tparam.Type().(*types2.TypeParam).Bound())
}
}
func (w *writer) typeParamNames(tparams []*types2.TypeName) {
w.sync(syncTypeParamNames)
for _, tparam := range tparams {
w.pos(tparam)
w.localIdent(tparam)
}
}
func (w *writer) method(meth *types2.Func) {
decl, ok := w.p.funDecls[meth]
assert(ok)
sig := meth.Type().(*types2.Signature)
w.sync(syncMethod)
w.pos(meth)
w.selector(meth)
w.typeParamNames(sig.RParams())
w.param(sig.Recv())
w.signature(sig)
w.pos(decl) // XXX: Hack to workaround linker limitations.
w.ext.funcExt(meth)
}
// qualifiedIdent writes out the name of an object declared at package
// scope. (For now, it's also used to refer to local defined types.)
func (w *writer) qualifiedIdent(obj types2.Object) {
w.sync(syncSym)
name := obj.Name()
if isDefinedType(obj) && obj.Pkg() == w.p.curpkg {
decl, ok := w.p.typDecls[obj.(*types2.TypeName)]
assert(ok)
if decl.gen != 0 {
// TODO(mdempsky): Find a better solution than embedding middle
// dot in the symbol name; this is terrible.
name = fmt.Sprintf("%s·%v", name, decl.gen)
}
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
}
w.pkg(obj.Pkg())
w.string(name)
}
// TODO(mdempsky): We should be able to omit pkg from both localIdent
// and selector, because they should always be known from context.
// However, past frustrations with this optimization in iexport make
// me a little nervous to try it again.
// localIdent writes the name of a locally declared object (i.e.,
// objects that can only be accessed by name, within the context of a
// particular function).
func (w *writer) localIdent(obj types2.Object) {
assert(!isGlobal(obj))
w.sync(syncLocalIdent)
w.pkg(obj.Pkg())
w.string(obj.Name())
}
// selector writes the name of a field or method (i.e., objects that
// can only be accessed using selector expressions).
func (w *writer) selector(obj types2.Object) {
w.sync(syncSelector)
w.pkg(obj.Pkg())
w.string(obj.Name())
}
// @@@ Compiler extensions
func (w *writer) funcExt(obj *types2.Func) {
decl, ok := w.p.funDecls[obj]
assert(ok)
// TODO(mdempsky): Extend these pragma validation flags to account
// for generics. E.g., linkname probably doesn't make sense at
// least.
pragma := asPragmaFlag(decl.Pragma)
if pragma&ir.Systemstack != 0 && pragma&ir.Nosplit != 0 {
w.p.errorf(decl, "go:nosplit and go:systemstack cannot be combined")
}
if decl.Body != nil {
if pragma&ir.Noescape != 0 {
w.p.errorf(decl, "can only use //go:noescape with external func implementations")
}
} else {
if base.Flag.Complete || decl.Name.Value == "init" {
// Linknamed functions are allowed to have no body. Hopefully
// the linkname target has a body. See issue 23311.
if _, ok := w.p.linknames[obj]; !ok {
w.p.errorf(decl, "missing function body")
}
}
}
sig, block := obj.Type().(*types2.Signature), decl.Body
body, closureVars := w.p.bodyIdx(w.p.curpkg, sig, block, w.dict)
assert(len(closureVars) == 0)
w.sync(syncFuncExt)
w.pragmaFlag(pragma)
w.linkname(obj)
w.bool(false) // stub extension
w.reloc(relocBody, body)
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
w.sync(syncEOF)
}
func (w *writer) typeExt(obj *types2.TypeName) {
decl, ok := w.p.typDecls[obj]
assert(ok)
w.sync(syncTypeExt)
w.pragmaFlag(asPragmaFlag(decl.Pragma))
// No LSym.SymIdx info yet.
w.int64(-1)
w.int64(-1)
}
func (w *writer) varExt(obj *types2.Var) {
w.sync(syncVarExt)
w.linkname(obj)
}
func (w *writer) linkname(obj types2.Object) {
w.sync(syncLinkname)
w.int64(-1)
w.string(w.p.linknames[obj])
}
func (w *writer) pragmaFlag(p ir.PragmaFlag) {
w.sync(syncPragma)
w.int(int(p))
}
// @@@ Function bodies
func (pw *pkgWriter) bodyIdx(pkg *types2.Package, sig *types2.Signature, block *syntax.BlockStmt, dict *writerDict) (idx int, closureVars []posObj) {
w := pw.newWriter(relocBody, syncFuncBody)
w.dict = dict
w.funcargs(sig)
if w.bool(block != nil) {
w.stmts(block.List)
w.pos(block.Rbrace)
}
return w.flush(), w.closureVars
}
func (w *writer) funcargs(sig *types2.Signature) {
do := func(params *types2.Tuple, result bool) {
for i := 0; i < params.Len(); i++ {
w.funcarg(params.At(i), result)
}
}
if recv := sig.Recv(); recv != nil {
w.funcarg(recv, false)
}
do(sig.Params(), false)
do(sig.Results(), true)
}
func (w *writer) funcarg(param *types2.Var, result bool) {
if param.Name() != "" || result {
w.addLocal(param)
}
}
func (w *writer) addLocal(obj *types2.Var) {
w.sync(syncAddLocal)
idx := len(w.localsIdx)
Matthew Dempsky
committed
if enableSync {
w.int(idx)
}
if w.localsIdx == nil {
w.localsIdx = make(map[*types2.Var]int)
}
w.localsIdx[obj] = idx
}
func (w *writer) useLocal(pos syntax.Pos, obj *types2.Var) {
w.sync(syncUseObjLocal)
if idx, ok := w.localsIdx[obj]; w.bool(ok) {
w.len(idx)
return
}
idx, ok := w.closureVarsIdx[obj]
if !ok {
if w.closureVarsIdx == nil {
w.closureVarsIdx = make(map[*types2.Var]int)
}
idx = len(w.closureVars)
w.closureVars = append(w.closureVars, posObj{pos, obj})
w.closureVarsIdx[obj] = idx
}
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
w.len(idx)
}
func (w *writer) openScope(pos syntax.Pos) {
w.sync(syncOpenScope)
w.pos(pos)
}
func (w *writer) closeScope(pos syntax.Pos) {
w.sync(syncCloseScope)
w.pos(pos)
w.closeAnotherScope()
}
func (w *writer) closeAnotherScope() {
w.sync(syncCloseAnotherScope)
}
// @@@ Statements
func (w *writer) stmt(stmt syntax.Stmt) {
var stmts []syntax.Stmt
if stmt != nil {
stmts = []syntax.Stmt{stmt}
}
w.stmts(stmts)
}
func (w *writer) stmts(stmts []syntax.Stmt) {
w.sync(syncStmts)
for _, stmt := range stmts {
w.stmt1(stmt)
}
w.code(stmtEnd)
w.sync(syncStmtsEnd)
}
func (w *writer) stmt1(stmt syntax.Stmt) {
switch stmt := stmt.(type) {
default:
w.p.unexpected("statement", stmt)
case nil, *syntax.EmptyStmt:
return
case *syntax.AssignStmt:
switch {
case stmt.Rhs == nil:
w.code(stmtIncDec)
w.op(binOps[stmt.Op])
w.expr(stmt.Lhs)
w.pos(stmt)
case stmt.Op != 0 && stmt.Op != syntax.Def:
w.code(stmtAssignOp)
w.op(binOps[stmt.Op])
w.expr(stmt.Lhs)
w.pos(stmt)
w.expr(stmt.Rhs)
default:
w.code(stmtAssign)
w.pos(stmt)
w.exprList(stmt.Rhs)
w.assignList(stmt.Lhs)
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
}
case *syntax.BlockStmt:
w.code(stmtBlock)
w.blockStmt(stmt)
case *syntax.BranchStmt:
w.code(stmtBranch)
w.pos(stmt)
w.op(branchOps[stmt.Tok])
w.optLabel(stmt.Label)
case *syntax.CallStmt:
w.code(stmtCall)
w.pos(stmt)
w.op(callOps[stmt.Tok])
w.expr(stmt.Call)
case *syntax.DeclStmt:
for _, decl := range stmt.DeclList {
w.declStmt(decl)
}
case *syntax.ExprStmt:
w.code(stmtExpr)
w.expr(stmt.X)
case *syntax.ForStmt:
w.code(stmtFor)
w.forStmt(stmt)
case *syntax.IfStmt:
w.code(stmtIf)
w.ifStmt(stmt)
case *syntax.LabeledStmt:
w.code(stmtLabel)
w.pos(stmt)
w.label(stmt.Label)
w.stmt1(stmt.Stmt)
case *syntax.ReturnStmt:
w.code(stmtReturn)
w.pos(stmt)
w.exprList(stmt.Results)
case *syntax.SelectStmt:
w.code(stmtSelect)
w.selectStmt(stmt)
case *syntax.SendStmt:
w.code(stmtSend)
w.pos(stmt)
w.expr(stmt.Chan)
w.expr(stmt.Value)
case *syntax.SwitchStmt:
w.code(stmtSwitch)
w.switchStmt(stmt)
}
}
func (w *writer) assignList(expr syntax.Expr) {
exprs := unpackListExpr(expr)
w.len(len(exprs))
for _, expr := range exprs {
if name, ok := expr.(*syntax.Name); ok && name.Value != "_" {
if obj, ok := w.p.info.Defs[name]; ok {
obj := obj.(*types2.Var)
w.bool(true)
w.pos(obj)
w.localIdent(obj)
w.typ(obj.Type())
// TODO(mdempsky): Minimize locals index size by deferring
// this until the variables actually come into scope.
w.addLocal(obj)
continue
}
}