The Go Programming Language

Source file src/pkg/runtime/type.go

     1	// Copyright 2009 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	/*
     6	 * Runtime type representation.
     7	 *
     8	 * The following files know the exact layout of these
     9	 * data structures and must be kept in sync with this file:
    10	 *
    11	 *	../../cmd/gc/reflect.c
    12	 *	../../cmd/ld/dwarf.c decodetype_*
    13	 *	../reflect/type.go
    14	 *	type.h
    15	 */
    16	
    17	package runtime
    18	
    19	import "unsafe"
    20	
    21	// The compiler can only construct empty interface values at
    22	// compile time; non-empty interface values get created
    23	// during initialization.  Type is an empty interface
    24	// so that the compiler can lay out references as data.
    25	type Type interface{}
    26	
    27	// All types begin with a few common fields needed for
    28	// the interface runtime.
    29	type commonType struct {
    30		size          uintptr // size in bytes
    31		hash          uint32  // hash of type; avoids computation in hash tables
    32		alg           uint8   // algorithm for copy+hash+cmp (../runtime/runtime.h:/AMEM)
    33		align         uint8   // alignment of variable with this type
    34		fieldAlign    uint8   // alignment of struct field with this type
    35		kind          uint8   // enumeration for C
    36		string        *string // string form; unnecessary  but undeniably useful
    37		*uncommonType         // (relatively) uncommon fields
    38		ptrToThis     *Type   // pointer to this type, if used in binary or has methods
    39	}
    40	
    41	// Values for commonType.kind.
    42	const (
    43		kindBool = 1 + iota
    44		kindInt
    45		kindInt8
    46		kindInt16
    47		kindInt32
    48		kindInt64
    49		kindUint
    50		kindUint8
    51		kindUint16
    52		kindUint32
    53		kindUint64
    54		kindUintptr
    55		kindFloat32
    56		kindFloat64
    57		kindComplex64
    58		kindComplex128
    59		kindArray
    60		kindChan
    61		kindFunc
    62		kindInterface
    63		kindMap
    64		kindPtr
    65		kindSlice
    66		kindString
    67		kindStruct
    68		kindUnsafePointer
    69	
    70		kindNoPointers = 1 << 7 // OR'ed into kind
    71	)
    72	
    73	// Method on non-interface type
    74	type _method struct { // underscore is to avoid collision with C
    75		name    *string        // name of method
    76		pkgPath *string        // nil for exported Names; otherwise import path
    77		mtyp    *Type          // method type (without receiver)
    78		typ     *Type          // .(*FuncType) underneath (with receiver)
    79		ifn     unsafe.Pointer // fn used in interface call (one-word receiver)
    80		tfn     unsafe.Pointer // fn used for normal method call
    81	}
    82	
    83	// uncommonType is present only for types with names or methods
    84	// (if T is a named type, the uncommonTypes for T and *T have methods).
    85	// Using a pointer to this struct reduces the overall size required
    86	// to describe an unnamed type with no methods.
    87	type uncommonType struct {
    88		name    *string   // name of type
    89		pkgPath *string   // import path; nil for built-in types like int, string
    90		methods []_method // methods associated with type
    91	}
    92	
    93	// BoolType represents a boolean type.
    94	type BoolType commonType
    95	
    96	// FloatType represents a float type.
    97	type FloatType commonType
    98	
    99	// ComplexType represents a complex type.
   100	type ComplexType commonType
   101	
   102	// IntType represents an int type.
   103	type IntType commonType
   104	
   105	// UintType represents a uint type.
   106	type UintType commonType
   107	
   108	// StringType represents a string type.
   109	type StringType commonType
   110	
   111	// UintptrType represents a uintptr type.
   112	type UintptrType commonType
   113	
   114	// UnsafePointerType represents an unsafe.Pointer type.
   115	type UnsafePointerType commonType
   116	
   117	// ArrayType represents a fixed array type.
   118	type ArrayType struct {
   119		commonType
   120		elem  *Type // array element type
   121		slice *Type // slice type
   122		len   uintptr
   123	}
   124	
   125	// SliceType represents a slice type.
   126	type SliceType struct {
   127		commonType
   128		elem *Type // slice element type
   129	}
   130	
   131	// ChanDir represents a channel type's direction.
   132	type ChanDir int
   133	
   134	const (
   135		RecvDir ChanDir             = 1 << iota // <-chan
   136		SendDir                                 // chan<-
   137		BothDir = RecvDir | SendDir             // chan
   138	)
   139	
   140	// ChanType represents a channel type.
   141	type ChanType struct {
   142		commonType
   143		elem *Type   // channel element type
   144		dir  uintptr // channel direction (ChanDir)
   145	}
   146	
   147	// FuncType represents a function type.
   148	type FuncType struct {
   149		commonType
   150		dotdotdot bool    // last input parameter is ...
   151		in        []*Type // input parameter types
   152		out       []*Type // output parameter types
   153	}
   154	
   155	// Method on interface type
   156	type _imethod struct { // underscore is to avoid collision with C
   157		name    *string // name of method
   158		pkgPath *string // nil for exported Names; otherwise import path
   159		typ     *Type   // .(*FuncType) underneath
   160	}
   161	
   162	// InterfaceType represents an interface type.
   163	type InterfaceType struct {
   164		commonType
   165		methods []_imethod // sorted by hash
   166	}
   167	
   168	// MapType represents a map type.
   169	type MapType struct {
   170		commonType
   171		key  *Type // map key type
   172		elem *Type // map element (value) type
   173	}
   174	
   175	// PtrType represents a pointer type.
   176	type PtrType struct {
   177		commonType
   178		elem *Type // pointer element (pointed at) type
   179	}
   180	
   181	// Struct field
   182	type structField struct {
   183		name    *string // nil for embedded fields
   184		pkgPath *string // nil for exported Names; otherwise import path
   185		typ     *Type   // type of field
   186		tag     *string // nil if no tag
   187		offset  uintptr // byte offset of field within struct
   188	}
   189	
   190	// StructType represents a struct type.
   191	type StructType struct {
   192		commonType
   193		fields []structField // sorted by offset
   194	}
   195	
   196	/*
   197	 * Must match iface.c:/Itab and compilers.
   198	 * NOTE: this is the version used by the reflection code, there is another
   199	 * one in iface_defs.go that is closer to the original C version.
   200	 */
   201	type Itable struct {
   202		Itype  *Type // (*tab.inter).(*InterfaceType) is the interface type
   203		Type   *Type
   204		link   *Itable
   205		bad    int32
   206		unused int32
   207		Fn     [100000]uintptr // bigger than we'll ever see
   208	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.