1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Indexed package export.
6//
7// The indexed export data format is an evolution of the previous
8// binary export data format. Its chief contribution is introducing an
9// index table, which allows efficient random access of individual
10// declarations and inline function bodies. In turn, this allows
11// avoiding unnecessary work for compilation units that import large
12// packages.
13//
14//
15// The top-level data format is structured as:
16//
17//     Header struct {
18//         Tag        byte   // 'i'
19//         Version    uvarint
20//         StringSize uvarint
21//         DataSize   uvarint
22//     }
23//
24//     Strings [StringSize]byte
25//     Data    [DataSize]byte
26//
27//     MainIndex []struct{
28//         PkgPath   stringOff
29//         PkgName   stringOff
30//         PkgHeight uvarint
31//
32//         Decls []struct{
33//             Name   stringOff
34//             Offset declOff
35//         }
36//     }
37//
38//     Fingerprint [8]byte
39//
40// uvarint means a uint64 written out using uvarint encoding.
41//
42// []T means a uvarint followed by that many T objects. In other
43// words:
44//
45//     Len   uvarint
46//     Elems [Len]T
47//
48// stringOff means a uvarint that indicates an offset within the
49// Strings section. At that offset is another uvarint, followed by
50// that many bytes, which form the string value.
51//
52// declOff means a uvarint that indicates an offset within the Data
53// section where the associated declaration can be found.
54//
55//
56// There are five kinds of declarations, distinguished by their first
57// byte:
58//
59//     type Var struct {
60//         Tag  byte // 'V'
61//         Pos  Pos
62//         Type typeOff
63//     }
64//
65//     type Func struct {
66//         Tag       byte // 'F' or 'G'
67//         Pos       Pos
68//         TypeParams []typeOff  // only present if Tag == 'G'
69//         Signature Signature
70//     }
71//
72//     type Const struct {
73//         Tag   byte // 'C'
74//         Pos   Pos
75//         Value Value
76//     }
77//
78//     type Type struct {
79//         Tag        byte // 'T' or 'U'
80//         Pos        Pos
81//         TypeParams []typeOff  // only present if Tag == 'U'
82//         Underlying typeOff
83//
84//         Methods []struct{  // omitted if Underlying is an interface type
85//             Pos       Pos
86//             Name      stringOff
87//             Recv      Param
88//             Signature Signature
89//         }
90//     }
91//
92//     type Alias struct {
93//         Tag  byte // 'A'
94//         Pos  Pos
95//         Type typeOff
96//     }
97//
98//     // "Automatic" declaration of each typeparam
99//     type TypeParam struct {
100//         Tag        byte // 'P'
101//         Pos        Pos
102//         Implicit   bool
103//         Constraint typeOff
104//     }
105//
106// typeOff means a uvarint that either indicates a predeclared type,
107// or an offset into the Data section. If the uvarint is less than
108// predeclReserved, then it indicates the index into the predeclared
109// types list (see predeclared in bexport.go for order). Otherwise,
110// subtracting predeclReserved yields the offset of a type descriptor.
111//
112// Value means a type, kind, and type-specific value. See
113// (*exportWriter).value for details.
114//
115//
116// There are twelve kinds of type descriptors, distinguished by an itag:
117//
118//     type DefinedType struct {
119//         Tag     itag // definedType
120//         Name    stringOff
121//         PkgPath stringOff
122//     }
123//
124//     type PointerType struct {
125//         Tag  itag // pointerType
126//         Elem typeOff
127//     }
128//
129//     type SliceType struct {
130//         Tag  itag // sliceType
131//         Elem typeOff
132//     }
133//
134//     type ArrayType struct {
135//         Tag  itag // arrayType
136//         Len  uint64
137//         Elem typeOff
138//     }
139//
140//     type ChanType struct {
141//         Tag  itag   // chanType
142//         Dir  uint64 // 1 RecvOnly; 2 SendOnly; 3 SendRecv
143//         Elem typeOff
144//     }
145//
146//     type MapType struct {
147//         Tag  itag // mapType
148//         Key  typeOff
149//         Elem typeOff
150//     }
151//
152//     type FuncType struct {
153//         Tag       itag // signatureType
154//         PkgPath   stringOff
155//         Signature Signature
156//     }
157//
158//     type StructType struct {
159//         Tag     itag // structType
160//         PkgPath stringOff
161//         Fields []struct {
162//             Pos      Pos
163//             Name     stringOff
164//             Type     typeOff
165//             Embedded bool
166//             Note     stringOff
167//         }
168//     }
169//
170//     type InterfaceType struct {
171//         Tag     itag // interfaceType
172//         PkgPath stringOff
173//         Embeddeds []struct {
174//             Pos  Pos
175//             Type typeOff
176//         }
177//         Methods []struct {
178//             Pos       Pos
179//             Name      stringOff
180//             Signature Signature
181//         }
182//     }
183//
184//     // Reference to a type param declaration
185//     type TypeParamType struct {
186//         Tag     itag // typeParamType
187//         Name    stringOff
188//         PkgPath stringOff
189//     }
190//
191//     // Instantiation of a generic type (like List[T2] or List[int])
192//     type InstanceType struct {
193//         Tag     itag // instanceType
194//         Pos     pos
195//         TypeArgs []typeOff
196//         BaseType typeOff
197//     }
198//
199//     type UnionType struct {
200//         Tag     itag // interfaceType
201//         Terms   []struct {
202//             tilde bool
203//             Type  typeOff
204//         }
205//     }
206//
207//
208//
209//     type Signature struct {
210//         Params   []Param
211//         Results  []Param
212//         Variadic bool  // omitted if Results is empty
213//     }
214//
215//     type Param struct {
216//         Pos  Pos
217//         Name stringOff
218//         Type typOff
219//     }
220//
221//
222// Pos encodes a file:line:column triple, incorporating a simple delta
223// encoding scheme within a data object. See exportWriter.pos for
224// details.
225//
226//
227// Compiler-specific details.
228//
229// cmd/compile writes out a second index for inline bodies and also
230// appends additional compiler-specific details after declarations.
231// Third-party tools are not expected to depend on these details and
232// they're expected to change much more rapidly, so they're omitted
233// here. See exportWriter's varExt/funcExt/etc methods for details.
234
235package typecheck
236
237import (
238	"strings"
239)
240
241const blankMarker = "$"
242
243// TparamName returns the real name of a type parameter, after stripping its
244// qualifying prefix and reverting blank-name encoding. See TparamExportName
245// for details.
246func TparamName(exportName string) string {
247	// Remove the "path" from the type param name that makes it unique.
248	ix := strings.LastIndex(exportName, ".")
249	if ix < 0 {
250		return ""
251	}
252	name := exportName[ix+1:]
253	if strings.HasPrefix(name, blankMarker) {
254		return "_"
255	}
256	return name
257}
258
259// The name used for dictionary parameters or local variables.
260const LocalDictName = ".dict"
261