/
bimport.go
1383 lines (1175 loc) · 31.3 KB
/
bimport.go
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
314
315
316
317
318
319
320
321
322
323
324
325
326
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
368
369
370
371
372
373
374
375
376
377
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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
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
644
645
646
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
683
684
685
686
687
688
689
690
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
749
750
751
752
753
754
755
756
757
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
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
918
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
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 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.
// Binary package import.
// See bexport.go for the export data format and how
// to make a format change.
package gc
import (
"bufio"
"cmd/compile/internal/types"
"cmd/internal/src"
"encoding/binary"
"fmt"
"math/big"
"strconv"
"strings"
)
// The overall structure of Import is symmetric to Export: For each
// export method in bexport.go there is a matching and symmetric method
// in bimport.go. Changing the export format requires making symmetric
// changes to bimport.go and bexport.go.
type importer struct {
in *bufio.Reader
imp *types.Pkg // imported package
buf []byte // reused for reading strings
version int // export format version
// object lists, in order of deserialization
strList []string
pathList []string
pkgList []*types.Pkg
typList []*types.Type
funcList []*Node // nil entry means already declared
trackAllTypes bool
// for delayed type verification
cmpList []struct{ pt, t *types.Type }
// position encoding
posInfoFormat bool
prevFile string
prevLine int
posBase *src.PosBase
// debugging support
debugFormat bool
read int // bytes read
}
// Import populates imp from the serialized package data read from in.
func Import(imp *types.Pkg, in *bufio.Reader) {
inimport = true
defer func() { inimport = false }()
p := importer{
in: in,
imp: imp,
version: -1, // unknown version
strList: []string{""}, // empty string is mapped to 0
pathList: []string{""}, // empty path is mapped to 0
}
// read version info
var versionstr string
if b := p.rawByte(); b == 'c' || b == 'd' {
// Go1.7 encoding; first byte encodes low-level
// encoding format (compact vs debug).
// For backward-compatibility only (avoid problems with
// old installed packages). Newly compiled packages use
// the extensible format string.
// TODO(gri) Remove this support eventually; after Go1.8.
if b == 'd' {
p.debugFormat = true
}
p.trackAllTypes = p.rawByte() == 'a'
p.posInfoFormat = p.bool()
versionstr = p.string()
if versionstr == "v1" {
p.version = 0
}
} else {
// Go1.8 extensible encoding
// read version string and extract version number (ignore anything after the version number)
versionstr = p.rawStringln(b)
if s := strings.SplitN(versionstr, " ", 3); len(s) >= 2 && s[0] == "version" {
if v, err := strconv.Atoi(s[1]); err == nil && v > 0 {
p.version = v
}
}
}
// read version specific flags - extend as necessary
switch p.version {
// case 6:
// ...
// fallthrough
case 5, 4, 3, 2, 1:
p.debugFormat = p.rawStringln(p.rawByte()) == "debug"
p.trackAllTypes = p.bool()
p.posInfoFormat = p.bool()
case 0:
// Go1.7 encoding format - nothing to do here
default:
p.formatErrorf("unknown export format version %d (%q)", p.version, versionstr)
}
// --- generic export data ---
// populate typList with predeclared "known" types
p.typList = append(p.typList, predeclared()...)
// read package data
p.pkg()
// defer some type-checking until all types are read in completely
tcok := typecheckok
typecheckok = true
defercheckwidth()
// read objects
// phase 1
objcount := 0
for {
tag := p.tagOrIndex()
if tag == endTag {
break
}
p.obj(tag)
objcount++
}
// self-verification
if count := p.int(); count != objcount {
p.formatErrorf("got %d objects; want %d", objcount, count)
}
// --- compiler-specific export data ---
// read compiler-specific flags
// phase 2
objcount = 0
for {
tag := p.tagOrIndex()
if tag == endTag {
break
}
p.obj(tag)
objcount++
}
// self-verification
if count := p.int(); count != objcount {
p.formatErrorf("got %d objects; want %d", objcount, count)
}
// read inlineable functions bodies
if dclcontext != PEXTERN {
p.formatErrorf("unexpected context %d", dclcontext)
}
objcount = 0
for i0 := -1; ; {
i := p.int() // index of function with inlineable body
if i < 0 {
break
}
// don't process the same function twice
if i <= i0 {
p.formatErrorf("index not increasing: %d <= %d", i, i0)
}
i0 = i
if funcdepth != 0 {
p.formatErrorf("unexpected Funcdepth %d", funcdepth)
}
// Note: In the original code, funchdr and funcbody are called for
// all functions (that were not yet imported). Now, we are calling
// them only for functions with inlineable bodies. funchdr does
// parameter renaming which doesn't matter if we don't have a body.
inlCost := p.int()
if f := p.funcList[i]; f != nil && f.Func.Inl.Len() == 0 {
// function not yet imported - read body and set it
funchdr(f)
body := p.stmtList()
if body == nil {
// Make sure empty body is not interpreted as
// no inlineable body (see also parser.fnbody)
// (not doing so can cause significant performance
// degradation due to unnecessary calls to empty
// functions).
body = []*Node{nod(OEMPTY, nil, nil)}
}
f.Func.Inl.Set(body)
f.Func.InlCost = int32(inlCost)
if Debug['E'] > 0 && Debug['m'] > 2 && f.Func.Inl.Len() != 0 {
if Debug['m'] > 3 {
fmt.Printf("inl body for %v: %+v\n", f, f.Func.Inl)
} else {
fmt.Printf("inl body for %v: %v\n", f, f.Func.Inl)
}
}
funcbody()
} else {
// function already imported - read body but discard declarations
dclcontext = PDISCARD // throw away any declarations
p.stmtList()
dclcontext = PEXTERN
}
objcount++
}
// self-verification
if count := p.int(); count != objcount {
p.formatErrorf("got %d functions; want %d", objcount, count)
}
if dclcontext != PEXTERN {
p.formatErrorf("unexpected context %d", dclcontext)
}
p.verifyTypes()
// --- end of export data ---
typecheckok = tcok
resumecheckwidth()
if debug_dclstack != 0 {
testdclstack()
}
}
func (p *importer) formatErrorf(format string, args ...interface{}) {
if debugFormat {
Fatalf(format, args...)
}
yyerror("cannot import %q due to version skew - reinstall package (%s)",
p.imp.Path, fmt.Sprintf(format, args...))
errorexit()
}
func (p *importer) verifyTypes() {
for _, pair := range p.cmpList {
pt := pair.pt
t := pair.t
if !eqtype(pt.Orig, t) {
p.formatErrorf("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, p.imp.Path)
}
}
}
// numImport tracks how often a package with a given name is imported.
// It is used to provide a better error message (by using the package
// path to disambiguate) if a package that appears multiple times with
// the same name appears in an error message.
var numImport = make(map[string]int)
func (p *importer) pkg() *types.Pkg {
// if the package was seen before, i is its index (>= 0)
i := p.tagOrIndex()
if i >= 0 {
return p.pkgList[i]
}
// otherwise, i is the package tag (< 0)
if i != packageTag {
p.formatErrorf("expected package tag, found tag = %d", i)
}
// read package data
name := p.string()
var path string
if p.version >= 5 {
path = p.path()
} else {
path = p.string()
}
// we should never see an empty package name
if name == "" {
p.formatErrorf("empty package name for path %q", path)
}
// we should never see a bad import path
if isbadimport(path, true) {
p.formatErrorf("bad package path %q for package %s", path, name)
}
// an empty path denotes the package we are currently importing;
// it must be the first package we see
if (path == "") != (len(p.pkgList) == 0) {
p.formatErrorf("package path %q for pkg index %d", path, len(p.pkgList))
}
// add package to pkgList
pkg := p.imp
if path != "" {
pkg = types.NewPkg(path, "")
}
if pkg.Name == "" {
pkg.Name = name
numImport[name]++
} else if pkg.Name != name {
yyerror("conflicting package names %s and %s for path %q", pkg.Name, name, path)
}
if myimportpath != "" && path == myimportpath {
yyerror("import %q: package depends on %q (import cycle)", p.imp.Path, path)
errorexit()
}
p.pkgList = append(p.pkgList, pkg)
return pkg
}
func idealType(typ *types.Type) *types.Type {
if typ.IsUntyped() {
// canonicalize ideal types
typ = types.Types[TIDEAL]
}
return typ
}
func (p *importer) obj(tag int) {
switch tag {
case constTag:
pos := p.pos()
sym := p.qualifiedName()
typ := p.typ()
val := p.value(typ)
importconst(p.imp, sym, idealType(typ), npos(pos, nodlit(val)))
case aliasTag:
pos := p.pos()
sym := p.qualifiedName()
typ := p.typ()
importalias(pos, p.imp, sym, typ)
case typeTag:
p.typ()
case varTag:
pos := p.pos()
sym := p.qualifiedName()
typ := p.typ()
importvar(pos, p.imp, sym, typ)
case funcTag:
pos := p.pos()
sym := p.qualifiedName()
params := p.paramList()
result := p.paramList()
sig := functypefield(nil, params, result)
importsym(p.imp, sym, ONAME)
if old := asNode(sym.Def); old != nil && old.Op == ONAME {
// function was imported before (via another import)
if !eqtype(sig, old.Type) {
p.formatErrorf("inconsistent definition for func %v during import\n\t%v\n\t%v", sym, old.Type, sig)
}
n := asNode(old.Type.Nname())
p.funcList = append(p.funcList, n)
break
}
n := newfuncnamel(pos, sym)
n.Type = sig
// TODO(mdempsky): Stop clobbering n.Pos in declare.
savedlineno := lineno
lineno = pos
declare(n, PFUNC)
lineno = savedlineno
p.funcList = append(p.funcList, n)
importlist = append(importlist, n)
sig.SetNname(asTypesNode(n))
if Debug['E'] > 0 {
fmt.Printf("import [%q] func %v \n", p.imp.Path, n)
}
default:
p.formatErrorf("unexpected object (tag = %d)", tag)
}
}
func (p *importer) pos() src.XPos {
if !p.posInfoFormat {
return src.NoXPos
}
file := p.prevFile
line := p.prevLine
delta := p.int()
line += delta
if p.version >= 5 {
if delta == deltaNewFile {
if n := p.int(); n >= 0 {
// file changed
file = p.path()
line = n
}
}
} else {
if delta == 0 {
if n := p.int(); n >= 0 {
// file changed
file = p.prevFile[:n] + p.string()
line = p.int()
}
}
}
if file != p.prevFile {
p.prevFile = file
p.posBase = src.NewFileBase(file, file)
}
p.prevLine = line
pos := src.MakePos(p.posBase, uint(line), 0)
xpos := Ctxt.PosTable.XPos(pos)
return xpos
}
func (p *importer) path() string {
// if the path was seen before, i is its index (>= 0)
// (the empty string is at index 0)
i := p.int()
if i >= 0 {
return p.pathList[i]
}
// otherwise, i is the negative path length (< 0)
a := make([]string, -i)
for n := range a {
a[n] = p.string()
}
s := strings.Join(a, "/")
p.pathList = append(p.pathList, s)
return s
}
func (p *importer) newtyp(etype types.EType) *types.Type {
t := types.New(etype)
if p.trackAllTypes {
p.typList = append(p.typList, t)
}
return t
}
// importtype declares that pt, an imported named type, has underlying type t.
func (p *importer) importtype(pt, t *types.Type) {
if pt.Etype == TFORW {
copytype(asNode(pt.Nod), t)
pt.Sym.Importdef = p.imp
pt.Sym.Lastlineno = lineno
declare(asNode(pt.Nod), PEXTERN)
checkwidth(pt)
} else {
// pt.Orig and t must be identical.
if p.trackAllTypes {
// If we track all types, t may not be fully set up yet.
// Collect the types and verify identity later.
p.cmpList = append(p.cmpList, struct{ pt, t *types.Type }{pt, t})
} else if !eqtype(pt.Orig, t) {
yyerror("inconsistent definition for type %v during import\n\t%L (in %q)\n\t%L (in %q)", pt.Sym, pt, pt.Sym.Importdef.Path, t, p.imp.Path)
}
}
if Debug['E'] != 0 {
fmt.Printf("import type %v %L\n", pt, t)
}
}
func (p *importer) typ() *types.Type {
// if the type was seen before, i is its index (>= 0)
i := p.tagOrIndex()
if i >= 0 {
return p.typList[i]
}
// otherwise, i is the type tag (< 0)
var t *types.Type
switch i {
case namedTag:
pos := p.pos()
tsym := p.qualifiedName()
t = pkgtype(pos, p.imp, tsym)
p.typList = append(p.typList, t)
dup := !t.IsKind(types.TFORW) // type already imported
// read underlying type
t0 := p.typ()
// TODO(mdempsky): Stop clobbering n.Pos in declare.
savedlineno := lineno
lineno = pos
p.importtype(t, t0)
lineno = savedlineno
// interfaces don't have associated methods
if t0.IsInterface() {
break
}
// set correct import context (since p.typ() may be called
// while importing the body of an inlined function)
savedContext := dclcontext
dclcontext = PEXTERN
// read associated methods
for i := p.int(); i > 0; i-- {
mpos := p.pos()
sym := p.fieldSym()
// during import unexported method names should be in the type's package
if !exportname(sym.Name) && sym.Pkg != tsym.Pkg {
Fatalf("imported method name %+v in wrong package %s\n", sym, tsym.Pkg.Name)
}
recv := p.paramList() // TODO(gri) do we need a full param list for the receiver?
params := p.paramList()
result := p.paramList()
nointerface := p.bool()
mt := functypefield(recv[0], params, result)
oldm := addmethod(sym, mt, false, nointerface)
if dup {
// An earlier import already declared this type and its methods.
// Discard the duplicate method declaration.
n := asNode(oldm.Type.Nname())
p.funcList = append(p.funcList, n)
continue
}
n := newfuncnamel(mpos, methodname(sym, recv[0].Type))
n.Type = mt
n.SetClass(PFUNC)
checkwidth(n.Type)
p.funcList = append(p.funcList, n)
importlist = append(importlist, n)
// (comment from parser.go)
// inl.C's inlnode in on a dotmeth node expects to find the inlineable body as
// (dotmeth's type).Nname.Inl, and dotmeth's type has been pulled
// out by typecheck's lookdot as this $$.ttype. So by providing
// this back link here we avoid special casing there.
mt.SetNname(asTypesNode(n))
if Debug['E'] > 0 {
fmt.Printf("import [%q] meth %v \n", p.imp.Path, n)
}
}
dclcontext = savedContext
case arrayTag:
t = p.newtyp(TARRAY)
bound := p.int64()
elem := p.typ()
t.Extra = &types.Array{Elem: elem, Bound: bound}
case sliceTag:
t = p.newtyp(TSLICE)
elem := p.typ()
t.Extra = types.Slice{Elem: elem}
case dddTag:
t = p.newtyp(TDDDFIELD)
t.Extra = types.DDDField{T: p.typ()}
case structTag:
t = p.newtyp(TSTRUCT)
t.SetFields(p.fieldList())
checkwidth(t)
case pointerTag:
t = p.newtyp(types.Tptr)
t.Extra = types.Ptr{Elem: p.typ()}
case signatureTag:
t = p.newtyp(TFUNC)
params := p.paramList()
result := p.paramList()
functypefield0(t, nil, params, result)
case interfaceTag:
if ml := p.methodList(); len(ml) == 0 {
t = types.Types[TINTER]
} else {
t = p.newtyp(TINTER)
t.SetInterface(ml)
}
case mapTag:
t = p.newtyp(TMAP)
mt := t.MapType()
mt.Key = p.typ()
mt.Val = p.typ()
case chanTag:
t = p.newtyp(TCHAN)
ct := t.ChanType()
ct.Dir = types.ChanDir(p.int())
ct.Elem = p.typ()
default:
p.formatErrorf("unexpected type (tag = %d)", i)
}
if t == nil {
p.formatErrorf("nil type (type tag = %d)", i)
}
return t
}
func (p *importer) qualifiedName() *types.Sym {
name := p.string()
pkg := p.pkg()
return pkg.Lookup(name)
}
func (p *importer) fieldList() (fields []*types.Field) {
if n := p.int(); n > 0 {
fields = make([]*types.Field, n)
for i := range fields {
fields[i] = p.field()
}
}
return
}
func (p *importer) field() *types.Field {
pos := p.pos()
sym, alias := p.fieldName()
typ := p.typ()
note := p.string()
f := types.NewField()
if sym.Name == "" {
// anonymous field: typ must be T or *T and T must be a type name
s := typ.Sym
if s == nil && typ.IsPtr() {
s = typ.Elem().Sym // deref
}
sym = sym.Pkg.Lookup(s.Name)
f.Embedded = 1
} else if alias {
// anonymous field: we have an explicit name because it's a type alias
f.Embedded = 1
}
f.Sym = sym
f.Nname = asTypesNode(newnamel(pos, sym))
f.Type = typ
f.Note = note
return f
}
func (p *importer) methodList() (methods []*types.Field) {
for n := p.int(); n > 0; n-- {
f := types.NewField()
f.Nname = asTypesNode(newname(nblank.Sym))
asNode(f.Nname).Pos = p.pos()
f.Type = p.typ()
methods = append(methods, f)
}
for n := p.int(); n > 0; n-- {
methods = append(methods, p.method())
}
return
}
func (p *importer) method() *types.Field {
pos := p.pos()
sym := p.methodName()
params := p.paramList()
result := p.paramList()
f := types.NewField()
f.Sym = sym
f.Nname = asTypesNode(newnamel(pos, sym))
f.Type = functypefield(fakeRecvField(), params, result)
return f
}
func (p *importer) fieldName() (*types.Sym, bool) {
name := p.string()
if p.version == 0 && name == "_" {
// version 0 didn't export a package for _ field names
// but used the builtin package instead
return builtinpkg.Lookup(name), false
}
pkg := localpkg
alias := false
switch name {
case "":
// 1) field name matches base type name and is exported: nothing to do
case "?":
// 2) field name matches base type name and is not exported: need package
name = ""
pkg = p.pkg()
case "@":
// 3) field name doesn't match base type name (alias name): need name and possibly package
name = p.string()
alias = true
fallthrough
default:
if !exportname(name) {
pkg = p.pkg()
}
}
return pkg.Lookup(name), alias
}
func (p *importer) methodName() *types.Sym {
name := p.string()
if p.version == 0 && name == "_" {
// version 0 didn't export a package for _ method names
// but used the builtin package instead
return builtinpkg.Lookup(name)
}
pkg := localpkg
if !exportname(name) {
pkg = p.pkg()
}
return pkg.Lookup(name)
}
func (p *importer) paramList() []*types.Field {
i := p.int()
if i == 0 {
return nil
}
// negative length indicates unnamed parameters
named := true
if i < 0 {
i = -i
named = false
}
// i > 0
fs := make([]*types.Field, i)
for i := range fs {
fs[i] = p.param(named)
}
return fs
}
func (p *importer) param(named bool) *types.Field {
f := types.NewField()
f.Type = p.typ()
if f.Type.Etype == TDDDFIELD {
// TDDDFIELD indicates wrapped ... slice type
f.Type = types.NewSlice(f.Type.DDDField())
f.SetIsddd(true)
}
if named {
name := p.string()
if name == "" {
p.formatErrorf("expected named parameter")
}
// TODO(gri) Supply function/method package rather than
// encoding the package for each parameter repeatedly.
pkg := localpkg
if name != "_" {
pkg = p.pkg()
}
f.Sym = pkg.Lookup(name)
f.Nname = asTypesNode(newname(f.Sym))
}
// TODO(gri) This is compiler-specific (escape info).
// Move into compiler-specific section eventually?
f.Note = p.string()
return f
}
func (p *importer) value(typ *types.Type) (x Val) {
switch tag := p.tagOrIndex(); tag {
case falseTag:
x.U = false
case trueTag:
x.U = true
case int64Tag:
u := new(Mpint)
u.SetInt64(p.int64())
u.Rune = typ == types.Idealrune
x.U = u
case floatTag:
f := newMpflt()
p.float(f)
if typ == types.Idealint || typ.IsInteger() {
// uncommon case: large int encoded as float
u := new(Mpint)
u.SetFloat(f)
x.U = u
break
}
x.U = f
case complexTag:
u := new(Mpcplx)
p.float(&u.Real)
p.float(&u.Imag)
x.U = u
case stringTag:
x.U = p.string()
case unknownTag:
p.formatErrorf("unknown constant (importing package with errors)")
case nilTag:
x.U = new(NilVal)
default:
p.formatErrorf("unexpected value tag %d", tag)
}
// verify ideal type
if typ.IsUntyped() && untype(x.Ctype()) != typ {
p.formatErrorf("value %v and type %v don't match", x, typ)
}
return
}
func (p *importer) float(x *Mpflt) {
sign := p.int()
if sign == 0 {
x.SetFloat64(0)
return
}
exp := p.int()
mant := new(big.Int).SetBytes([]byte(p.string()))
m := x.Val.SetInt(mant)
m.SetMantExp(m, exp-mant.BitLen())
if sign < 0 {
m.Neg(m)
}
}
// ----------------------------------------------------------------------------
// Inlined function bodies
// Approach: Read nodes and use them to create/declare the same data structures
// as done originally by the (hidden) parser by closely following the parser's
// original code. In other words, "parsing" the import data (which happens to
// be encoded in binary rather textual form) is the best way at the moment to
// re-establish the syntax tree's invariants. At some future point we might be
// able to avoid this round-about way and create the rewritten nodes directly,
// possibly avoiding a lot of duplicate work (name resolution, type checking).
//
// Refined nodes (e.g., ODOTPTR as a refinement of OXDOT) are exported as their
// unrefined nodes (since this is what the importer uses). The respective case
// entries are unreachable in the importer.
func (p *importer) stmtList() []*Node {
var list []*Node
for {
n := p.node()
if n == nil {
break
}
// OBLOCK nodes may be created when importing ODCL nodes - unpack them
if n.Op == OBLOCK {
list = append(list, n.List.Slice()...)
} else {
list = append(list, n)
}
}
return list
}
func (p *importer) exprList() []*Node {
var list []*Node
for {
n := p.expr()
if n == nil {
break
}
list = append(list, n)
}
return list
}
func (p *importer) elemList() []*Node {
c := p.int()
list := make([]*Node, c)
for i := range list {
s := p.fieldSym()
list[i] = nodSym(OSTRUCTKEY, p.expr(), s)
}
return list
}
func (p *importer) expr() *Node {
n := p.node()
if n != nil && n.Op == OBLOCK {
Fatalf("unexpected block node: %v", n)
}
return n
}
func npos(pos src.XPos, n *Node) *Node {
n.Pos = pos
return n
}
// TODO(gri) split into expr and stmt
func (p *importer) node() *Node {
switch op := p.op(); op {
// expressions
// case OPAREN:
// unreachable - unpacked by exporter
// case ODDDARG:
// unimplemented
case OLITERAL:
pos := p.pos()
typ := p.typ()
n := npos(pos, nodlit(p.value(typ)))
if !typ.IsUntyped() {
// Type-checking simplifies unsafe.Pointer(uintptr(c))
// to unsafe.Pointer(c) which then cannot type-checked
// again. Re-introduce explicit uintptr(c) conversion.
// (issue 16317).
if typ.IsUnsafePtr() {
n = nodl(pos, OCONV, n, nil)
n.Type = types.Types[TUINTPTR]
}
n = nodl(pos, OCONV, n, nil)
n.Type = typ
}
return n
case ONAME:
return npos(p.pos(), mkname(p.sym()))
// case OPACK, ONONAME:
// unreachable - should have been resolved by typechecking
case OTYPE:
return npos(p.pos(), typenod(p.typ()))
// case OTARRAY, OTMAP, OTCHAN, OTSTRUCT, OTINTER, OTFUNC:
// unreachable - should have been resolved by typechecking
// case OCLOSURE:
// unimplemented
case OPTRLIT:
pos := p.pos()
n := npos(pos, p.expr())
if !p.bool() /* !implicit, i.e. '&' operator */ {
if n.Op == OCOMPLIT {
// Special case for &T{...}: turn into (*T){...}.
n.Right = nodl(pos, OIND, n.Right, nil)
n.Right.SetImplicit(true)
} else {
n = nodl(pos, OADDR, n, nil)
}
}
return n
case OSTRUCTLIT:
// TODO(mdempsky): Export position information for OSTRUCTKEY nodes.
savedlineno := lineno
lineno = p.pos()
n := nodl(lineno, OCOMPLIT, nil, typenod(p.typ()))
n.List.Set(p.elemList()) // special handling of field names
lineno = savedlineno
return n
// case OARRAYLIT, OSLICELIT, OMAPLIT:
// unreachable - mapped to case OCOMPLIT below by exporter
case OCOMPLIT: