Source file src/cmd/compile/internal/typecheck/iexport.go

     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' or 'B'
    94  //         Pos  Pos
    95  //         TypeParams []typeOff  // only present if Tag == 'B'
    96  //         Type typeOff
    97  //     }
    98  //
    99  //     // "Automatic" declaration of each typeparam
   100  //     type TypeParam struct {
   101  //         Tag        byte // 'P'
   102  //         Pos        Pos
   103  //         Implicit   bool
   104  //         Constraint typeOff
   105  //     }
   106  //
   107  // typeOff means a uvarint that either indicates a predeclared type,
   108  // or an offset into the Data section. If the uvarint is less than
   109  // predeclReserved, then it indicates the index into the predeclared
   110  // types list (see predeclared in bexport.go for order). Otherwise,
   111  // subtracting predeclReserved yields the offset of a type descriptor.
   112  //
   113  // Value means a type, kind, and type-specific value. See
   114  // (*exportWriter).value for details.
   115  //
   116  //
   117  // There are twelve kinds of type descriptors, distinguished by an itag:
   118  //
   119  //     type DefinedType struct {
   120  //         Tag     itag // definedType
   121  //         Name    stringOff
   122  //         PkgPath stringOff
   123  //     }
   124  //
   125  //     type PointerType struct {
   126  //         Tag  itag // pointerType
   127  //         Elem typeOff
   128  //     }
   129  //
   130  //     type SliceType struct {
   131  //         Tag  itag // sliceType
   132  //         Elem typeOff
   133  //     }
   134  //
   135  //     type ArrayType struct {
   136  //         Tag  itag // arrayType
   137  //         Len  uint64
   138  //         Elem typeOff
   139  //     }
   140  //
   141  //     type ChanType struct {
   142  //         Tag  itag   // chanType
   143  //         Dir  uint64 // 1 RecvOnly; 2 SendOnly; 3 SendRecv
   144  //         Elem typeOff
   145  //     }
   146  //
   147  //     type MapType struct {
   148  //         Tag  itag // mapType
   149  //         Key  typeOff
   150  //         Elem typeOff
   151  //     }
   152  //
   153  //     type FuncType struct {
   154  //         Tag       itag // signatureType
   155  //         PkgPath   stringOff
   156  //         Signature Signature
   157  //     }
   158  //
   159  //     type StructType struct {
   160  //         Tag     itag // structType
   161  //         PkgPath stringOff
   162  //         Fields []struct {
   163  //             Pos      Pos
   164  //             Name     stringOff
   165  //             Type     typeOff
   166  //             Embedded bool
   167  //             Note     stringOff
   168  //         }
   169  //     }
   170  //
   171  //     type InterfaceType struct {
   172  //         Tag     itag // interfaceType
   173  //         PkgPath stringOff
   174  //         Embeddeds []struct {
   175  //             Pos  Pos
   176  //             Type typeOff
   177  //         }
   178  //         Methods []struct {
   179  //             Pos       Pos
   180  //             Name      stringOff
   181  //             Signature Signature
   182  //         }
   183  //     }
   184  //
   185  //     // Reference to a type param declaration
   186  //     type TypeParamType struct {
   187  //         Tag     itag // typeParamType
   188  //         Name    stringOff
   189  //         PkgPath stringOff
   190  //     }
   191  //
   192  //     // Instantiation of a generic type (like List[T2] or List[int])
   193  //     type InstanceType struct {
   194  //         Tag     itag // instanceType
   195  //         Pos     pos
   196  //         TypeArgs []typeOff
   197  //         BaseType typeOff
   198  //     }
   199  //
   200  //     type UnionType struct {
   201  //         Tag     itag // interfaceType
   202  //         Terms   []struct {
   203  //             tilde bool
   204  //             Type  typeOff
   205  //         }
   206  //     }
   207  //
   208  //
   209  //
   210  //     type Signature struct {
   211  //         Params   []Param
   212  //         Results  []Param
   213  //         Variadic bool  // omitted if Results is empty
   214  //     }
   215  //
   216  //     type Param struct {
   217  //         Pos  Pos
   218  //         Name stringOff
   219  //         Type typOff
   220  //     }
   221  //
   222  //
   223  // Pos encodes a file:line:column triple, incorporating a simple delta
   224  // encoding scheme within a data object. See exportWriter.pos for
   225  // details.
   226  //
   227  //
   228  // Compiler-specific details.
   229  //
   230  // cmd/compile writes out a second index for inline bodies and also
   231  // appends additional compiler-specific details after declarations.
   232  // Third-party tools are not expected to depend on these details and
   233  // they're expected to change much more rapidly, so they're omitted
   234  // here. See exportWriter's varExt/funcExt/etc methods for details.
   235  
   236  package typecheck
   237  
   238  import (
   239  	"strings"
   240  )
   241  
   242  const blankMarker = "$"
   243  
   244  // TparamName returns the real name of a type parameter, after stripping its
   245  // qualifying prefix and reverting blank-name encoding. See TparamExportName
   246  // for details.
   247  func TparamName(exportName string) string {
   248  	// Remove the "path" from the type param name that makes it unique.
   249  	ix := strings.LastIndex(exportName, ".")
   250  	if ix < 0 {
   251  		return ""
   252  	}
   253  	name := exportName[ix+1:]
   254  	if strings.HasPrefix(name, blankMarker) {
   255  		return "_"
   256  	}
   257  	return name
   258  }
   259  
   260  // The name used for dictionary parameters or local variables.
   261  const LocalDictName = ".dict"
   262  

View as plain text