github.com/aykevl/tinygo@v0.5.0/src/reflect/type.go (about)

     1  package reflect
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  // The compiler uses a compact encoding to store type information. Unlike the
     8  // main Go compiler, most of the types are stored directly in the type code.
     9  //
    10  // Type code bit allocation:
    11  // xxxxx0: basic types, where xxxxx is the basic type number (never 0).
    12  //         The higher bits indicate the named type, if any.
    13  //  nxxx1: complex types, where n indicates whether this is a named type (named
    14  //         if set) and xxx contains the type kind number:
    15  //             0 (0001): Chan
    16  //             1 (0011): Interface
    17  //             2 (0101): Ptr
    18  //             3 (0111): Slice
    19  //             4 (1001): Array
    20  //             5 (1011): Func
    21  //             6 (1101): Map
    22  //             7 (1111): Struct
    23  //         The higher bits are either the contents of the type depending on the
    24  //         type (if n is clear) or indicate the number of the named type (if n
    25  //         is set).
    26  
    27  type Kind uintptr
    28  
    29  // Copied from reflect/type.go
    30  // https://golang.org/src/reflect/type.go?s=8302:8316#L217
    31  const (
    32  	Invalid Kind = iota
    33  	Bool
    34  	Int
    35  	Int8
    36  	Int16
    37  	Int32
    38  	Int64
    39  	Uint
    40  	Uint8
    41  	Uint16
    42  	Uint32
    43  	Uint64
    44  	Uintptr
    45  	Float32
    46  	Float64
    47  	Complex64
    48  	Complex128
    49  	String
    50  	UnsafePointer
    51  	Chan
    52  	Interface
    53  	Ptr
    54  	Slice
    55  	Array
    56  	Func
    57  	Map
    58  	Struct
    59  )
    60  
    61  func (k Kind) String() string {
    62  	switch k {
    63  	case Bool:
    64  		return "bool"
    65  	case Int:
    66  		return "int"
    67  	case Int8:
    68  		return "int8"
    69  	case Int16:
    70  		return "int16"
    71  	case Int32:
    72  		return "int32"
    73  	case Int64:
    74  		return "int64"
    75  	case Uint:
    76  		return "uint"
    77  	case Uint8:
    78  		return "uint8"
    79  	case Uint16:
    80  		return "uint16"
    81  	case Uint32:
    82  		return "uint32"
    83  	case Uint64:
    84  		return "uint64"
    85  	case Uintptr:
    86  		return "uintptr"
    87  	case Float32:
    88  		return "float32"
    89  	case Float64:
    90  		return "float64"
    91  	case Complex64:
    92  		return "complex64"
    93  	case Complex128:
    94  		return "complex128"
    95  	case String:
    96  		return "string"
    97  	case UnsafePointer:
    98  		return "unsafe.Pointer"
    99  	case Chan:
   100  		return "chan"
   101  	case Interface:
   102  		return "interface"
   103  	case Ptr:
   104  		return "ptr"
   105  	case Slice:
   106  		return "slice"
   107  	case Array:
   108  		return "array"
   109  	case Func:
   110  		return "func"
   111  	case Map:
   112  		return "map"
   113  	case Struct:
   114  		return "struct"
   115  	default:
   116  		return "invalid"
   117  	}
   118  }
   119  
   120  // basicType returns a new Type for this kind if Kind is a basic type.
   121  func (k Kind) basicType() Type {
   122  	return Type(k << 1)
   123  }
   124  
   125  // The typecode as used in an interface{}.
   126  type Type uintptr
   127  
   128  func TypeOf(i interface{}) Type {
   129  	return ValueOf(i).typecode
   130  }
   131  
   132  func (t Type) String() string {
   133  	return "T"
   134  }
   135  
   136  func (t Type) Kind() Kind {
   137  	if t%2 == 0 {
   138  		// basic type
   139  		return Kind((t >> 1) % 32)
   140  	} else {
   141  		return Kind(t>>1)%8 + 19
   142  	}
   143  }
   144  
   145  func (t Type) Elem() Type {
   146  	switch t.Kind() {
   147  	case Chan, Ptr, Slice:
   148  		if (t>>4)%2 != 0 {
   149  			panic("unimplemented: (reflect.Type).Elem() for named types")
   150  		}
   151  		return t >> 5
   152  	default: // not implemented: Array, Map
   153  		panic("unimplemented: (reflect.Type).Elem()")
   154  	}
   155  }
   156  
   157  func (t Type) Field(i int) StructField {
   158  	panic("unimplemented: (reflect.Type).Field()")
   159  }
   160  
   161  func (t Type) Bits() int {
   162  	panic("unimplemented: (reflect.Type).Bits()")
   163  }
   164  
   165  func (t Type) Len() int {
   166  	panic("unimplemented: (reflect.Type).Len()")
   167  }
   168  
   169  func (t Type) NumField() int {
   170  	panic("unimplemented: (reflect.Type).NumField()")
   171  }
   172  
   173  func (t Type) Size() uintptr {
   174  	switch t.Kind() {
   175  	case Bool, Int8, Uint8:
   176  		return 1
   177  	case Int16, Uint16:
   178  		return 2
   179  	case Int32, Uint32:
   180  		return 4
   181  	case Int64, Uint64:
   182  		return 8
   183  	case Int, Uint:
   184  		return unsafe.Sizeof(int(0))
   185  	case Uintptr:
   186  		return unsafe.Sizeof(uintptr(0))
   187  	case Float32:
   188  		return 4
   189  	case Float64:
   190  		return 8
   191  	case Complex64:
   192  		return 8
   193  	case Complex128:
   194  		return 16
   195  	case String:
   196  		return unsafe.Sizeof(StringHeader{})
   197  	case UnsafePointer, Chan, Map, Ptr:
   198  		return unsafe.Sizeof(uintptr(0))
   199  	case Slice:
   200  		return unsafe.Sizeof(SliceHeader{})
   201  	default:
   202  		panic("unimplemented: size of type")
   203  	}
   204  }
   205  
   206  type StructField struct {
   207  	Name string
   208  	Type Type
   209  }