...
1
2
3
4
5
6
7 package gcimporter
8
9 import (
10 "fmt"
11 "go/token"
12 "go/types"
13 "internal/pkgbits"
14 "sync"
15 )
16
17 func assert(b bool) {
18 if !b {
19 panic("assertion failed")
20 }
21 }
22
23 func errorf(format string, args ...any) {
24 panic(fmt.Sprintf(format, args...))
25 }
26
27
28
29
30 const deltaNewFile = -64
31
32
33 type fakeFileSet struct {
34 fset *token.FileSet
35 files map[string]*fileInfo
36 }
37
38 type fileInfo struct {
39 file *token.File
40 lastline int
41 }
42
43 const maxlines = 64 * 1024
44
45 func (s *fakeFileSet) pos(file string, line, column int) token.Pos {
46
47
48
49
50
51
52 f := s.files[file]
53 if f == nil {
54 f = &fileInfo{file: s.fset.AddFile(file, -1, maxlines)}
55 s.files[file] = f
56 }
57
58 if line > maxlines {
59 line = 1
60 }
61 if line > f.lastline {
62 f.lastline = line
63 }
64
65
66 return token.Pos(f.file.Base() + line - 1)
67 }
68
69 func (s *fakeFileSet) setLines() {
70 fakeLinesOnce.Do(func() {
71 fakeLines = make([]int, maxlines)
72 for i := range fakeLines {
73 fakeLines[i] = i
74 }
75 })
76 for _, f := range s.files {
77 f.file.SetLines(fakeLines[:f.lastline])
78 }
79 }
80
81 var (
82 fakeLines []int
83 fakeLinesOnce sync.Once
84 )
85
86 func chanDir(d int) types.ChanDir {
87
88 switch d {
89 case 1 :
90 return types.RecvOnly
91 case 2 :
92 return types.SendOnly
93 case 3 :
94 return types.SendRecv
95 default:
96 errorf("unexpected channel dir %d", d)
97 return 0
98 }
99 }
100
101 var predeclared = []types.Type{
102
103 types.Typ[types.Bool],
104 types.Typ[types.Int],
105 types.Typ[types.Int8],
106 types.Typ[types.Int16],
107 types.Typ[types.Int32],
108 types.Typ[types.Int64],
109 types.Typ[types.Uint],
110 types.Typ[types.Uint8],
111 types.Typ[types.Uint16],
112 types.Typ[types.Uint32],
113 types.Typ[types.Uint64],
114 types.Typ[types.Uintptr],
115 types.Typ[types.Float32],
116 types.Typ[types.Float64],
117 types.Typ[types.Complex64],
118 types.Typ[types.Complex128],
119 types.Typ[types.String],
120
121
122 types.Universe.Lookup("byte").Type(),
123 types.Universe.Lookup("rune").Type(),
124
125
126 types.Universe.Lookup("error").Type(),
127
128
129 types.Typ[types.UntypedBool],
130 types.Typ[types.UntypedInt],
131 types.Typ[types.UntypedRune],
132 types.Typ[types.UntypedFloat],
133 types.Typ[types.UntypedComplex],
134 types.Typ[types.UntypedString],
135 types.Typ[types.UntypedNil],
136
137
138 types.Typ[types.UnsafePointer],
139
140
141 types.Typ[types.Invalid],
142
143
144
145 anyType{},
146
147
148 types.Universe.Lookup("comparable").Type(),
149
150
151 }
152
153 type anyType struct{}
154
155 func (t anyType) Underlying() types.Type { return t }
156 func (t anyType) String() string { return "any" }
157
158
159 type derivedInfo struct {
160 idx pkgbits.Index
161 needed bool
162 }
163
164
165 type typeInfo struct {
166 idx pkgbits.Index
167 derived bool
168 }
169
170
171 func splitVargenSuffix(name string) (base, suffix string) {
172 i := len(name)
173 for i > 0 && name[i-1] >= '0' && name[i-1] <= '9' {
174 i--
175 }
176 const dot = "ยท"
177 if i >= len(dot) && name[i-len(dot):i] == dot {
178 i -= len(dot)
179 return name[:i], name[i:]
180 }
181 return name, ""
182 }
183
View as plain text