Source file src/internal/abi/compiletype.go

     1  // Copyright 2023 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  package abi
     6  
     7  // These functions are the build-time version of the Go type data structures.
     8  
     9  // Their contents must be kept in sync with their definitions.
    10  // Because the host and target type sizes can differ, the compiler and
    11  // linker cannot use the host information that they might get from
    12  // either unsafe.Sizeof and Alignof, nor runtime, reflect, or reflectlite.
    13  
    14  // CommonSize returns sizeof(Type) for a compilation target with a given ptrSize
    15  func CommonSize(ptrSize int) int { return 4*ptrSize + 8 + 8 }
    16  
    17  // StructFieldSize returns sizeof(StructField) for a compilation target with a given ptrSize
    18  func StructFieldSize(ptrSize int) int { return 3 * ptrSize }
    19  
    20  // UncommonSize returns sizeof(UncommonType).  This currently does not depend on ptrSize.
    21  // This exported function is in an internal package, so it may change to depend on ptrSize in the future.
    22  func UncommonSize() uint64 { return 4 + 2 + 2 + 4 + 4 }
    23  
    24  // TFlagOff returns the offset of Type.TFlag for a compilation target with a given ptrSize
    25  func TFlagOff(ptrSize int) int { return 2*ptrSize + 4 }
    26  
    27  // ITabTypeOff returns the offset of ITab.Type for a compilation target with a given ptrSize
    28  func ITabTypeOff(ptrSize int) int { return ptrSize }
    29  
    30  // RTypeSize returns sizeof(kindType) for a compilation target with a given ptrSize.
    31  func RTypeSize(kind Kind, ptrSize int) int {
    32  	cs := CommonSize(ptrSize)
    33  	switch kind {
    34  	case Struct: // reflect.structType
    35  		return cs + 4*ptrSize
    36  	case Pointer: // reflect.ptrType
    37  		return cs + ptrSize
    38  	case Func: // reflect.funcType
    39  		return cs + ptrSize // 4 bytes, pointer aligned
    40  	case Slice: // reflect.sliceType
    41  		return cs + ptrSize
    42  	case Array: // reflect.arrayType
    43  		return cs + 3*ptrSize
    44  	case Chan: // reflect.chanType
    45  		return cs + 2*ptrSize
    46  	case Map: // internal/abi.MapType
    47  		sz := cs + 10*ptrSize + 4
    48  		if ptrSize == 8 {
    49  			sz += 4 // padding for final uint32 field (Flags).
    50  		}
    51  		return sz
    52  	case Interface: // reflect.interfaceType
    53  		return cs + 4*ptrSize
    54  	default:
    55  		// just Sizeof(rtype)
    56  		return cs
    57  	}
    58  }
    59  

View as plain text