diff options
Diffstat (limited to 'src/pkg/reflect/type.go')
| -rw-r--r-- | src/pkg/reflect/type.go | 322 |
1 files changed, 161 insertions, 161 deletions
diff --git a/src/pkg/reflect/type.go b/src/pkg/reflect/type.go index a6988d312c..2c059924bf 100644 --- a/src/pkg/reflect/type.go +++ b/src/pkg/reflect/type.go @@ -16,9 +16,9 @@ package reflect import ( - "runtime"; - "strconv"; - "unsafe"; + "runtime" + "strconv" + "unsafe" ) /* @@ -35,198 +35,198 @@ import ( */ type commonType struct { - size uintptr; - hash uint32; - alg uint8; - align uint8; - fieldAlign uint8; - string *string; - *uncommonType; + size uintptr + hash uint32 + alg uint8 + align uint8 + fieldAlign uint8 + string *string + *uncommonType } type method struct { - hash uint32; - name *string; - pkgPath *string; - typ *runtime.Type; - ifn unsafe.Pointer; - tfn unsafe.Pointer; + hash uint32 + name *string + pkgPath *string + typ *runtime.Type + ifn unsafe.Pointer + tfn unsafe.Pointer } type uncommonType struct { - name *string; - pkgPath *string; - methods []method; + name *string + pkgPath *string + methods []method } // BoolType represents a boolean type. type BoolType struct { - commonType; + commonType } // Float32Type represents a float32 type. type Float32Type struct { - commonType; + commonType } // Float64Type represents a float64 type. type Float64Type struct { - commonType; + commonType } // FloatType represents a float type. type FloatType struct { - commonType; + commonType } // Int16Type represents an int16 type. type Int16Type struct { - commonType; + commonType } // Int32Type represents an int32 type. type Int32Type struct { - commonType; + commonType } // Int64Type represents an int64 type. type Int64Type struct { - commonType; + commonType } // Int8Type represents an int8 type. type Int8Type struct { - commonType; + commonType } // IntType represents an int type. type IntType struct { - commonType; + commonType } // Uint16Type represents a uint16 type. type Uint16Type struct { - commonType; + commonType } // Uint32Type represents a uint32 type. type Uint32Type struct { - commonType; + commonType } // Uint64Type represents a uint64 type. type Uint64Type struct { - commonType; + commonType } // Uint8Type represents a uint8 type. type Uint8Type struct { - commonType; + commonType } // UintType represents a uint type. type UintType struct { - commonType; + commonType } // StringType represents a string type. type StringType struct { - commonType; + commonType } // UintptrType represents a uintptr type. type UintptrType struct { - commonType; + commonType } // DotDotDotType represents the ... that can // be used as the type of the final function parameter. type DotDotDotType struct { - commonType; + commonType } // UnsafePointerType represents an unsafe.Pointer type. type UnsafePointerType struct { - commonType; + commonType } // ArrayType represents a fixed array type. type ArrayType struct { - commonType; - elem *runtime.Type; - len uintptr; + commonType + elem *runtime.Type + len uintptr } // ChanDir represents a channel type's direction. type ChanDir int const ( - RecvDir ChanDir = 1 << iota; - SendDir; - BothDir = RecvDir | SendDir; + RecvDir ChanDir = 1 << iota + SendDir + BothDir = RecvDir | SendDir ) // ChanType represents a channel type. type ChanType struct { - commonType; - elem *runtime.Type; - dir uintptr; + commonType + elem *runtime.Type + dir uintptr } // FuncType represents a function type. type FuncType struct { - commonType; - in []*runtime.Type; - out []*runtime.Type; + commonType + in []*runtime.Type + out []*runtime.Type } // Method on interface type type imethod struct { - hash uint32; - perm uint32; - name *string; - pkgPath *string; - typ *runtime.Type; + hash uint32 + perm uint32 + name *string + pkgPath *string + typ *runtime.Type } // InterfaceType represents an interface type. type InterfaceType struct { - commonType; - methods []imethod; + commonType + methods []imethod } // MapType represents a map type. type MapType struct { - commonType; - key *runtime.Type; - elem *runtime.Type; + commonType + key *runtime.Type + elem *runtime.Type } // PtrType represents a pointer type. type PtrType struct { - commonType; - elem *runtime.Type; + commonType + elem *runtime.Type } // SliceType represents a slice type. type SliceType struct { - commonType; - elem *runtime.Type; + commonType + elem *runtime.Type } // Struct field type structField struct { - name *string; - pkgPath *string; - typ *runtime.Type; - tag *string; - offset uintptr; + name *string + pkgPath *string + typ *runtime.Type + tag *string + offset uintptr } // StructType represents a struct type. type StructType struct { - commonType; - fields []structField; + commonType + fields []structField } @@ -237,10 +237,10 @@ type StructType struct { // Method represents a single method. type Method struct { - PkgPath string; // empty for uppercase Name - Name string; - Type *FuncType; - Func *FuncValue; + PkgPath string // empty for uppercase Name + Name string + Type *FuncType + Func *FuncValue } // Type is the runtime representation of a Go type. @@ -253,37 +253,37 @@ type Type interface { // PkgPath returns the type's package path. // The package path is a full package import path like "container/vector". // PkgPath returns an empty string for unnamed types. - PkgPath() string; + PkgPath() string // Name returns the type's name within its package. // Name returns an empty string for unnamed types. - Name() string; + Name() string // String returns a string representation of the type. // The string representation may use shortened package names // (e.g., vector instead of "container/vector") and is not // guaranteed to be unique among types. To test for equality, // compare the Types directly. - String() string; + String() string // Size returns the number of bytes needed to store // a value of the given type; it is analogous to unsafe.Sizeof. - Size() uintptr; + Size() uintptr // Align returns the alignment of a value of this type // when allocated in memory. - Align() int; + Align() int // FieldAlign returns the alignment of a value of this type // when used as a field in a struct. - FieldAlign() int; + FieldAlign() int // For non-interface types, Method returns the i'th method with receiver T. // For interface types, Method returns the i'th method in the interface. // NumMethod returns the number of such methods. - Method(int) Method; - NumMethod() int; - uncommon() *uncommonType; + Method(int) Method + NumMethod() int + uncommon() *uncommonType } func (t *uncommonType) uncommon() *uncommonType { @@ -294,70 +294,70 @@ func (t *uncommonType) PkgPath() string { if t == nil || t.pkgPath == nil { return "" } - return *t.pkgPath; + return *t.pkgPath } func (t *uncommonType) Name() string { if t == nil || t.name == nil { return "" } - return *t.name; + return *t.name } -func (t *commonType) String() string { return *t.string } +func (t *commonType) String() string { return *t.string } -func (t *commonType) Size() uintptr { return t.size } +func (t *commonType) Size() uintptr { return t.size } -func (t *commonType) Align() int { return int(t.align) } +func (t *commonType) Align() int { return int(t.align) } -func (t *commonType) FieldAlign() int { return int(t.fieldAlign) } +func (t *commonType) FieldAlign() int { return int(t.fieldAlign) } func (t *uncommonType) Method(i int) (m Method) { if t == nil || i < 0 || i >= len(t.methods) { return } - p := &t.methods[i]; + p := &t.methods[i] if p.name != nil { m.Name = *p.name } if p.pkgPath != nil { m.PkgPath = *p.pkgPath } - m.Type = toType(*p.typ).(*FuncType); - fn := p.tfn; - m.Func = newFuncValue(m.Type, addr(&fn), true); - return; + m.Type = toType(*p.typ).(*FuncType) + fn := p.tfn + m.Func = newFuncValue(m.Type, addr(&fn), true) + return } func (t *uncommonType) NumMethod() int { if t == nil { return 0 } - return len(t.methods); + return len(t.methods) } // TODO(rsc): 6g supplies these, but they are not // as efficient as they could be: they have commonType // as the receiver instead of *commonType. -func (t *commonType) NumMethod() int { return t.uncommonType.NumMethod() } +func (t *commonType) NumMethod() int { return t.uncommonType.NumMethod() } -func (t *commonType) Method(i int) (m Method) { return t.uncommonType.Method(i) } +func (t *commonType) Method(i int) (m Method) { return t.uncommonType.Method(i) } -func (t *commonType) PkgPath() string { return t.uncommonType.PkgPath() } +func (t *commonType) PkgPath() string { return t.uncommonType.PkgPath() } -func (t *commonType) Name() string { return t.uncommonType.Name() } +func (t *commonType) Name() string { return t.uncommonType.Name() } // Len returns the number of elements in the array. -func (t *ArrayType) Len() int { return int(t.len) } +func (t *ArrayType) Len() int { return int(t.len) } // Elem returns the type of the array's elements. -func (t *ArrayType) Elem() Type { return toType(*t.elem) } +func (t *ArrayType) Elem() Type { return toType(*t.elem) } // Dir returns the channel direction. -func (t *ChanType) Dir() ChanDir { return ChanDir(t.dir) } +func (t *ChanType) Dir() ChanDir { return ChanDir(t.dir) } // Elem returns the channel's element type. -func (t *ChanType) Elem() Type { return toType(*t.elem) } +func (t *ChanType) Elem() Type { return toType(*t.elem) } func (d ChanDir) String() string { switch d { @@ -368,7 +368,7 @@ func (d ChanDir) String() string { case BothDir: return "chan" } - return "ChanDir" + strconv.Itoa(int(d)); + return "ChanDir" + strconv.Itoa(int(d)) } // In returns the type of the i'th function input parameter. @@ -376,60 +376,60 @@ func (t *FuncType) In(i int) Type { if i < 0 || i >= len(t.in) { return nil } - return toType(*t.in[i]); + return toType(*t.in[i]) } // NumIn returns the number of input parameters. -func (t *FuncType) NumIn() int { return len(t.in) } +func (t *FuncType) NumIn() int { return len(t.in) } // Out returns the type of the i'th function output parameter. func (t *FuncType) Out(i int) Type { if i < 0 || i >= len(t.out) { return nil } - return toType(*t.out[i]); + return toType(*t.out[i]) } // NumOut returns the number of function output parameters. -func (t *FuncType) NumOut() int { return len(t.out) } +func (t *FuncType) NumOut() int { return len(t.out) } // Method returns the i'th interface method. func (t *InterfaceType) Method(i int) (m Method) { if i < 0 || i >= len(t.methods) { return } - p := &t.methods[i]; - m.Name = *p.name; + p := &t.methods[i] + m.Name = *p.name if p.pkgPath != nil { m.PkgPath = *p.pkgPath } - m.Type = toType(*p.typ).(*FuncType); - return; + m.Type = toType(*p.typ).(*FuncType) + return } // NumMethod returns the number of interface methods. -func (t *InterfaceType) NumMethod() int { return len(t.methods) } +func (t *InterfaceType) NumMethod() int { return len(t.methods) } // Key returns the map key type. -func (t *MapType) Key() Type { return toType(*t.key) } +func (t *MapType) Key() Type { return toType(*t.key) } // Elem returns the map element type. -func (t *MapType) Elem() Type { return toType(*t.elem) } +func (t *MapType) Elem() Type { return toType(*t.elem) } // Elem returns the pointer element type. -func (t *PtrType) Elem() Type { return toType(*t.elem) } +func (t *PtrType) Elem() Type { return toType(*t.elem) } // Elem returns the type of the slice's elements. -func (t *SliceType) Elem() Type { return toType(*t.elem) } +func (t *SliceType) Elem() Type { return toType(*t.elem) } type StructField struct { - PkgPath string; // empty for uppercase Name - Name string; - Type Type; - Tag string; - Offset uintptr; - Index []int; - Anonymous bool; + PkgPath string // empty for uppercase Name + Name string + Type Type + Tag string + Offset uintptr + Index []int + Anonymous bool } // Field returns the i'th struct field. @@ -437,17 +437,17 @@ func (t *StructType) Field(i int) (f StructField) { if i < 0 || i >= len(t.fields) { return } - p := t.fields[i]; - f.Type = toType(*p.typ); + p := t.fields[i] + f.Type = toType(*p.typ) if p.name != nil { f.Name = *p.name } else { - t := f.Type; + t := f.Type if pt, ok := t.(*PtrType); ok { t = pt.Elem() } - f.Name = t.Name(); - f.Anonymous = true; + f.Name = t.Name() + f.Anonymous = true } if p.pkgPath != nil { f.PkgPath = *p.pkgPath @@ -455,9 +455,9 @@ func (t *StructType) Field(i int) (f StructField) { if p.tag != nil { f.Tag = *p.tag } - f.Offset = p.offset; - f.Index = []int{i}; - return; + f.Offset = p.offset + f.Index = []int{i} + return } // TODO(gri): Should there be an error/bool indicator if the index @@ -467,45 +467,45 @@ func (t *StructType) Field(i int) (f StructField) { func (t *StructType) FieldByIndex(index []int) (f StructField) { for i, x := range index { if i > 0 { - ft := f.Type; + ft := f.Type if pt, ok := ft.(*PtrType); ok { ft = pt.Elem() } if st, ok := ft.(*StructType); ok { t = st } else { - var f0 StructField; - f = f0; - return; + var f0 StructField + f = f0 + return } } - f = t.Field(x); + f = t.Field(x) } - return; + return } -const inf = 1 << 30 // infinity - no struct has that many nesting levels +const inf = 1 << 30 // infinity - no struct has that many nesting levels func (t *StructType) fieldByName(name string, mark map[*StructType]bool, depth int) (ff StructField, fd int) { - fd = inf; // field depth + fd = inf // field depth if _, marked := mark[t]; marked { // Struct already seen. return } - mark[t] = true; + mark[t] = true - var fi int; // field index - n := 0; // number of matching fields at depth fd -L: for i, _ := range t.fields { - f := t.Field(i); - d := inf; + var fi int // field index + n := 0 // number of matching fields at depth fd +L: for i, _ := range t.fields { + f := t.Field(i) + d := inf switch { case f.Name == name: // Matching top-level field. d = depth case f.Anonymous: - ft := f.Type; + ft := f.Type if pt, ok := ft.(*PtrType); ok { ft = pt.Elem() } @@ -524,12 +524,12 @@ L: for i, _ := range t.fields { switch { case d < fd: // Found field at shallower depth. - ff, fi, fd = f, i, d; - n = 1; + ff, fi, fd = f, i, d + n = 1 case d == fd: // More than one matching field at the same depth (or d, fd == inf). // Same as no field found at this depth. - n++; + n++ if d == depth { // Impossible to find a field at lower depth. break L @@ -542,28 +542,28 @@ L: for i, _ := range t.fields { if len(ff.Index) <= depth { ff.Index = make([]int, depth+1) } - ff.Index[depth] = fi; + ff.Index[depth] = fi } else { // None or more than one matching field found. fd = inf } - mark[t] = false, false; - return; + mark[t] = false, false + return } // FieldByName returns the struct field with the given name // and a boolean to indicate if the field was found. func (t *StructType) FieldByName(name string) (f StructField, present bool) { if ff, fd := t.fieldByName(name, make(map[*StructType]bool), 0); fd < inf { - ff.Index = ff.Index[0 : fd+1]; - f, present = ff, true; + ff.Index = ff.Index[0 : fd+1] + f, present = ff, true } - return; + return } // NumField returns the number of struct fields. -func (t *StructType) NumField() int { return len(t.fields) } +func (t *StructType) NumField() int { return len(t.fields) } // Convert runtime type to reflect type. // Same memory layouts, different method sets. @@ -624,15 +624,15 @@ func toType(i interface{}) Type { case *runtime.StructType: return (*StructType)(unsafe.Pointer(v)) } - panicln("toType", i); + panicln("toType", i) } // ArrayOrSliceType is the common interface implemented // by both ArrayType and SliceType. type ArrayOrSliceType interface { - Type; - Elem() Type; + Type + Elem() Type } // Typeof returns the reflection Type of the value in the interface{}. -func Typeof(i interface{}) Type { return toType(unsafe.Typeof(i)) } +func Typeof(i interface{}) Type { return toType(unsafe.Typeof(i)) } |
